I\'ve been wrestling with an issue I was hoping to solve with regex.
Let\'s say I have a string that can contain any alphanumeric with the possibility of a substrin
How to do this depends on what should be done with:
a b [ c [ d [ e ] f ] g
That is ambiguous; possible answers are at least:
ab[ c [ d [ e ] f ]g
ab[ c [ d [ e ]f]g
For the first two cases, you can use regexps. For the third case, you'd be much better off with a (small) parser.
For either case one or two, split the string on the first [
. Strip spaces from everything before [
(that's obviously outside of the brackets). Next, look for .*\]
(case 1) or .*?\]
(case 2) and move that over to your output. Repeat until you're out of input.