I have trouble splitting string based on regex.
String str = "1=(1-2,3-4),2=2,3=3,4=4";
Pattern commaPattern = Pattern.compile("\\([0-9-]+,[0-9-]+\\)|(,)") ;
String[] arr = commaPattern.split(str);
for (String s : arr)
{
System.out.println(s);
}
Expected output,
1=(1-2,3-4)
2=2
3=3
4=4
Actual output,
1=
2=2
3=3
4=4
This regex would split as required
,(?![^()]*\\))
------------
|->split with , only if it is not within ()
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