2

I need to do the following regex in Java:

Split a string at each comma that's not preceded by a backslash (ie. escaped) and is followed by zero or more whitespaces.

I've been trying this:

String str = "Name=Doe\, Jane, Hobby=Skiing, Height=1.70";
String[] parts = str.split("[^\\],\s*");

which is the correct syntax in Perl and works there. Not so in Java.

The above already throws an exception during compilation:

error: illegal escape character
    String[] parts = str.split("[^\\],\s*");

Adding a third and fourth backslash in the character class doesn't help

str.split("[^\\\\],\s*");

Adding a second backslash to the whitespace allows it to compile,

String[] parts = str.split("[^\\],\\s*");

but then a runtime regex.PatternSyntaxException occurs, stating an unclosed character class:

java.util.regex.PatternSyntaxException: Unclosed character class
near index 7
[^\],\s*
       ^

Clearly there's a backslash missing, and I can't get it in ... Can anybody tell me how this should be done in Java?

thx!

0

1 Answer 1

0

This regex does what you want. You forgot to add two additional \\:

String[] parts = str.split("[^\\\\],\\s*");

Like explained in this question: (java regex pattern unclosed character class)

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

3 Comments

Ah, yes ... makes sense. Just goes to show that random trial and error is not a good idea ... thanks!
when should I add double backslash? When I want to split String using "," , I find I should write it like str.split("\\."); But if I splits String using "/" , I do not have to add double backslash in front of it. Why this happens? Could you help me with that?
@SHUYULYU The double backslash is used for escaping, so if your regex does contain a backslash you need to escape it otherwise it handles the single backslash wrong because it is a special sign in regex. When you use a normal string you can also use the @ sign in front of the string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.