split method uses regex as a parameter, and | in regex, a special character that means OR. To make it a normal \\ character before he likes it
"yourString".split("\\|");
In your case, you'll need look-ahead , so your regular expression might look like
/></|(/>)?\\|(?=[^>]*(</|$))(</)?
It will be broken into
/></| with optional /> before or </ after it, BUT ONLY if there isnโt > after it before </ or the end of your $ input. This ensures that | is out </ />
Also, to get rid of problems in situations like "</a|b/>|c|</d|e/>" , where </ is at the beginning and /> at the end of your input, you need to delete them before separation.
This seems necessary because we do not want to create an empty string as the first or last element in the created array, as in the case of "ab".split("a") , which will produce {"", "b"}
Allows you to check it:
for (String s : "</a0|b0/>|1|f1|</a1|a2/></a3|a4/>|f2|</a5|a6/>" .replaceAll("^</", "").split("/></|/>$|(/>)?\\|(?=[^>]*(</|$))(</)?")) { System.out.println(s); }
output:
a0|b0 1 f1 a1|a2 a3|a4 f2 a5|a6
source share