I’m trying to split strings in a rather specific way. I’ve been fooling around using the .split() and .replaceall() methods, but I can’t get it right.
Here are a couple of examples of the strings I need to split, followed by how they must be after the splitting. A , signifies a new string in the array.
Example 1: "(and (or (can-hit-robot) (wall) ) (can-hit-robot) (wall) ) )"
"(and", "(or", "(can-hit-robot)", "(wall)", ")", "(can-hit-robot)", "(wall)", ")"
Example 2: "(seq (shoot) (if (can-hit-robot) (shoot) (move) ) )"
"(seq", "(shoot)", "(if", "(can-hit-robot)", "(shoot)", "(move)", ")", ")"
Example 3: "(while(wall)(if (can-hit-robot)(shoot)(move)))"
"(while", "(wall)", "(if", "(can-hit-robot)", "(shoot)", "(move)", ")", ")"
Any help would be hugely appreciated!
How’s this?
It relies on lookbehind though, so engines without lookbehind might not be able to handle this expression. 🙁
The rule being expressed is, split just before an opening parenthesis and just after a closing parenthesis, also cutting off any spaces on the outside of the parenthesis. The left part of the alternation thus matches spaces leading up to an opening paren; the right part will match spaces continuing after a closing paren.