a bit of an overkill answer:
start with a somewhat more involved example. The string is keyed in using control-_
for subscripts etc.
{"Y", "!(*SubscriptBox[(X)", " (u)])", \
"!(*SubscriptBox[(X)", " (p)])", "X", \
"!(*TagBox[SubsuperscriptBox[\"x\"", " \"0\"", " \"n\"]", "
DisplayForm])", "p"}
this is a mess because StringSplit
has operated on all the commas embedded in the format code.
so.. reassemble by joining parts until they are valid strings. (see https://mathematica.stackexchange.com/a/102969/2079 )
validstring[s_String] :=
Nand[StringMatchQ[s, ___ ~~ "\!\(" ~~ ___], ToString[s] === s ]
Reap[last = Fold[ If[validstring[#1], (Sow[#1]; #2),
p = StringJoin[#1, ",", #2]] & , First@badsplit ,
Rest@badsplit]; If[validstring[last], Sow[last]]][[2, 1]]
Of course the best approach is to not use StringSplit
in the first place.
( See comments )
ToString[#, StandardForm] & /@ ToExpression[ "{" <> # <> "}"] &@myString
$\endgroup$FullForm
on your string you will see the underlying plain text string contains several commas.StringSplit
splits on those and so the result would be pretty hard to undo. $\endgroup$