MW.
MW.

Reputation: 564

Java split() regular expression

I like to split a string into an array by using the split(regex) function.

I want to split it on semicolons ; - but there are also "escaped" semicolons in the String (\;) which should not be used for the split.

Is there a regex for the .split(regex) function that would do this?

Upvotes: 1

Views: 251

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213223

Use negative look-behind to split on semi-colon not preceded by \\: -

str.split("(?<!\\\\);");

You need to use 4 backslashes - escape a backslash once for Java, and then escape the 2 backslashes again for regex.

Upvotes: 6

Related Questions