Reputation: 55
I have tried the following and the o/p is expected.
String valueIn = test-3-4-HH{3-4}-FF{38-99}
String[] valueInSplit = valueIn.split("-");
o/p array = [test, 3, 4, HH{3, 4}, FF{38, 99}]
Is it possible in regex to to create a patter which avoids the "-" between "{" and "}" so the expected o/p should be
o/p array = [test, 3, 4, HH{3-4}, FF{38-99}]
Is there any other way to do it. Kindly help.
Upvotes: 1
Views: 71
Reputation: 56809
It is possible, with the assumption that there is only 1 level of bracket {}
, and the brackets are open and closed properly. Use this regex with split
function:
-(?![^{}]*})
As String literal:
"-(?![^{}]*})"
The regex checks that the -
is currently not inside bracket, by checking whether we can find a closing bracket }
from the current position of the string, given that the character in between does not have an opening bracket {
or closing bracket }
.
Upvotes: 1
Reputation: 5268
You can achieve this using a negative lookahead in your split. Basically you check if there is a number and a closing bracket after the seperator -
.
Here is what I ended up with:
-(?!\d+\})
Upvotes: 1