I need to split a string into a list of parts in Ruby, but I need to ignore stuff inside paramentheses. For example:
A +4, B +6, C (hello, goodbye) +5, D +3
I’d like the resulting list to be:
[0]A +4
[1]B +6
[2]C (hello, goodbye) +5
[3]D +3
But I can’t simply split on commas, because that would split the contents of the parentheses. Is there a way to split stuff out without pre-parsing the commas in the braces into something else?
Thanks.
Try this:
Output:
A short explanation:
Another option is to split on a comma followed by some spaces only when the first parenthesis that can be seen when looking ahead is an opening parenthesis (or no parenthesis at all: ie. the end of the string):
will produce the same output, but I find the
scanmethod cleaner.