Tag Info

New answers tagged

9

As no one gave a FixedPoint answer, here is one: preparedStr = StringReplace[ "((your[drink {remember to}]) ovaltine)", { RegularExpression["[{[(]"] -> "{", RegularExpression["[)\]}]"] -> "}" }] "{{your{drink {remember to}}} ovaltine}" lst = {}; ...


34

In this response, I will focus upon the programming paradigm change when moving from Java to Mathematica. I will emphasize two differences between the languages. The first concerns the "feel" of writing Mathematica code. The second is about how iteration is expressed. The "Feel" of Mathematica Java is a reasonably conventional programming language, ...


4

str = "[can {and it(it (mix) up)} look silly]" paren = {{"\\(", "\\)"}, {"\\[", "\\]"}, {"{", "}"}}; allparen = StringJoin@Flatten[paren]; re = RegularExpression[ StringTake[ StringJoin[#[[1]] <> "[^" <> allparen <> "]*" <> #[[2]] <> "|" & /@paren] , {1, -2}]]; sr := Function[strl, Fold[ (Sow[" " ...


15

StringReplace method After reading other answers I was inspired to write a new method. I place it first because it is almost as concise as the method below yet it is more robust (and safe) because it preserves strings as strings. str = "[can {and it(it (mix) up)} look silly]"; StringReplace[str, {"["|"{"|"(" -> -1, "]"|"}"|")" -> 1, " " -> 0}] ...


10

This is a straightforward attempt at a recursive descent parser, favoring readability over brevity. First, the tokenizer: tokenize[str_] := DeleteCases[StringCases[str, { "(" -> open[1], "[" -> open[2], "{" -> open[3], ")" -> close[1], "]" -> close[2], "}" -> close[3], x : (Except[Characters["()[]{}"]] ..) ...


14

str = "[can {and it(it (mix) up)} look silly]"; i = 10; StringJoin @@ Last[Replace[Characters@str, {"[" | "(" | "{" :> Sow[" ", --i], "]" | ")" | "}" :> Sow["", ++i], c_ :> Sow[c, i]} , 1] ~Reap~ Range@10] (* " mix it up and it can look silly" *) This just scans through the characters one at a time and Sows them with an integer tag. ...


6

str = "[to {quite similar(answer (My) is)} Kuba's answer]"; mid = StringReplace[ StringReplace[ "Hold@" <> str, {"[" | "(" -> "{", "]" | ")" -> "}"}] // ToExpression // InputForm // ToString, "*" -> ","] // ToExpression // ReleaseHold {to, {quite, similar, {answer, {My}, is}}, Derivative[1][Kuba], s, answer} ...


5

This will give you an idea to start with I think: target = "[racket for {brackets (matching) is a} computers]" Rest@Reap[ Nest[With[{s = StringPosition[#, {"{", "(", "["}][[-1, 1]], e = StringPosition[#, {"}", ")", "]"}][[1, 1]]}, Sow[StringTake[#, {s + 1, e - 1}]]; StringDrop[#, {s, e}]] &, target, Length@StringPosition[target, ...


8

Reset the kernel first. str = "[can {and it(it (mix) up)} look silly]" new = StringReplace[ StringReplace[str, {"(" | "[" -> "{", ")" | "]" -> "}"}], {(a : WordCharacter ~~ " " | "" ~~ "{") :> a <> ",{", (a : WordCharacter ~~ " " ~~ b : WordCharacter) :> a <> "," <> b, ("}" ~~ " " | "" ~~ b : ...



Top 50 recent answers are included