tags:

views:

39

answers:

1

I'm looking to split space-delimited strings into a series of search terms. However, in doing so I'd like to ignore spaces within parentheses. For example, I'd like to be able to split the string

a, b, c, search:(1, 2, 3), d

into

[[a] [b] [c] [search:(1, 2, 3)] [d]]

Does anyone know how to do this using regular expressions in Java?

Thanks!

+1  A: 

This isn't a full regex, but it'll get you there:

(\([^)]*\)|\S)*

This uses a common trick, treating one long string of characters as if it were a single character. On the right side we match non-whitespace characters with \S. On the left side we match a balanced set of parentheses with anything in between.

The end result is that a balanced set of parentheses is treated as if it were a single character, and so the regex as a whole matches a single word, where a word can contain these parenthesized groups.

(Note that because this is a regular expression it can't handle nested parentheses. One set of parentheses is the limit.)

John Kugelman