3
String delimiterRegexp = "(;|:|[^<]/)";
String value = "get/time/pick me <i>Jack</i>";
String[] splitedTexts = value.split(delimiterRegexp);
for (String text : splitedTexts) {
System.out.println(text);
}

Output:
ge
tim
pick me <i>Jack</i>

Expected Result: 
get
time
pick me <i>Jack</i>

A character is getting added as delimeter along with /. Could anyone help me out to write regex to split text based on delimeter"/" and it should ignore xml end tag"

2 Answers 2

4

Your regex should be like this:

(;|:|(?<!<)/)

with a negative lookbehind, demo: https://regex101.com/r/2k1WI5/1/

Your current regex [^<]/ will match basically any character that is not < followed by / even \n, space, and Japanese characters.

That's why you are losing some letters as they are considered as part of the separator.

Following The fourth bird recommendation, you can even simplify the regex into: ([;:]|(?<!<)/)

Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps you could also reduce the first alternation to ([;:]|(?<!<)/)
@Thefourthbird: Thanks! It's even more beautiful!
3

[^<]/ will match e/ and t/

use a lookbehind instead, it will have the wanted behaviour to only consider / as separator if it's not a closing tag

On regex101.com

(?<!<)/

The whole regex

(;|:|(?<!<)/)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.