java regex double-backslash in character class -


this question has answer here:

i need following regex in java:

split string @ each comma that's not preceded backslash (ie. escaped) , followed 0 or more whitespaces.

i've been trying this:

string str = "name=doe\, jane, hobby=skiing, height=1.70"; string[] parts = str.split("[^\\],\s*"); 

which correct syntax in perl , works there. not in java.

the above throws exception during compilation:

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

adding third , fourth backslash in character class doesn't help

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

adding second backslash whitespace allows compile,

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

but runtime regex.patternsyntaxexception occurs, stating unclosed character class:

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

clearly there's backslash missing, , can't in ... can tell me how should done in java?

thx!

this regex want. forgot add 2 additional \\:

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

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


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -