Alex Rivera | Logout

Split string by all spaces except those in brackets

Asked 2012-10-14T17:14:47.020
10

Possible Duplicate:
Split a String based on regex

I've never been a regular expression guru, so I need your help! I have a string like this:

String s = "a [b c] d [e f g]";

I want to split this string using spaces as delimiters -- but I don't want to split on spaces that appear within the [] brackets. So, from the example above, I would like this array:

{"a", "[b c]", "d", "[e f g]"}

Any advice on what regex could be used in conjunction with split in order to achieve this?


Here's another example:

"[a b] c [[d e] f g]"

becomes

{"[a b]", "c", "[[d e] f g]"}
Edit
Report

1 Answer

-1

FOR NON NESTED

\\s+(?![^\\[]*\\])

FOR NESTED([] inside [])

(?<!\\[[^\\]]*)\\s+(?![^\\[]*\\])
answered 2012-10-14T17:27:47.297

Your Answer