user1262381
user1262381

Reputation: 13

Java splitting a string while ignoring any delimiters between brackets

I'm looking for java regex to split the inputString while ignoring delimiters between brackets/multiple brackets.

Java code:

Pattern p = Pattern.compile("[,regex?]");
String[] desiredOutput =p.split(inputString);

for example:

inputString="1,{2,{3},4},{4,5},6";

desiredOutput with ; delimiter:

1;{2,{3},4};{4,5};6

Thnx!

Upvotes: 1

Views: 1523

Answers (1)

aioobe
aioobe

Reputation: 420951

That's not possible using regular expressions.

The expression would have to match strings with balanced brackets. (Which is not a regular language.)

You need to use some other parsing technique or do it manually by counting { and }.

Upvotes: 5

Related Questions