I would like to split a string in java on a comma(,) but whenever the comma(,) is in between some parenthesis, it should not be split.
e.g. The string :
"life, would, (last , if ), all"
Should yield:
-life
-would
-(last , if )
-all
When I use :
String text = "life, would, (last , if ), all"
text.split(",");
I end up dividing the whole text even the (last , if ) I can see that split takes a regex but I can't seem to think of how to make it do the job.
you could use this pattern - (not for nested parenthesis)
,(?![^()]*\))
Demo
, # ","
(?! # Negative Look-Ahead
[^()] # Character not in [()] Character Class
* # (zero or more)(greedy)
\ #
) # End of Negative Look-Ahead
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With