Java string split regex non-capturing group

1.2k views Asked by At

I am trying to split the string on "." Except when a part of the string is in [ ] then I just want to return what is inside the brackets.

I have the following line of code:

"blah.blah[http://blah.blah.com/blah/blah#]".split(("(\\.|\\[(?=.*\\]))")

This returns:

 [ "blah", "blah", "http:blah", "blah", "com/blah/blah#]" ]

If instead I try:

"blah.blah[http://blah.blah.com/blah/blah#]".split(("(\\.|\\[(?:.*\\]))")

I get:

["blah", "blah"]

I'm not sure how I need to define my non capturing group so that it will split on the first [ but not capture anything after up to and including the ]

Just to clarify the array I am expecting back is

["blah", "blah", "http://blah.blah.com/blah/blah#"]
1

There are 1 answers

7
Casimir et Hippolyte On BEST ANSWER

To do that, the best option is to use the "find" method instead of split with this pattern:

(?<=\\[)[^\\]]*(?=\\])|[^\\][.]+

Note that the order of the alternatives is important because the first win. So (?<=\\[)[^\\]]*(?=\\]) must be before [^\\][.]+

demo