Question Warriors Question Warriors - 7 months ago 27
Java Question

Conditionally splitting a string on a complex delimiter

I have string


1,2,3|5,4,5|8


I want output like this :


1,2,3|5

4,5|8


How can i achieve this using java
i mean to split with "," but "," which is after "|"

Answer

Add a positive lookbehind condition, for example:

"1,2,3|5,4,5|8".split("(?<=\\|\\d{1,10}),"))

Lookbehind condition (?<=\\|\\d{1,10}) means: | followed by up to 10 digits (as lookbehind requires defined maximum length, unbounded repetition modifiers as + or * cannot be used).