java - Exclude strings within parentheses from a regular expression? -


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

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

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

Learn how to use regular expressions in Java

Thanks!

This is not a full regex, but you will find it there:

 < Code> (\ [[^]] * \) | \ S) *  

It uses a common trick, treats a long string of letters as if it was a single character on the right, we can write the non-whitsy characters on the \ S . On the left side we match a set of brackets with anything in between.

The end result is that a balanced set of brackets is considered as it is a single character, and therefore regex as a whole

(Note that this is a regular expression Because it can not handle nested brackets. There is a set limit of brackets.)


Comments