Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex predefined character class?

Tags:

java

regex

I have this regex, which attempts to combine the predefined character class \s with , :

String rgx = "[^\s,]"

But I get an error saying that \s is an illegal escape character. I can't double backslash \s, because if I do then the character class it will be interpreted as a backslash and a letter 's'. What can I do?

like image 978
George Newton Avatar asked Oct 09 '13 10:10

George Newton


People also ask

What does \\ mean in Java regex?

Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.

What does the character class \D denote in a regex expression?

\D – matches any character except a digit e.g., a letter. \S – matches any character except a whitespace e.g., a letter. \W – matches any character except a word character e.g., non-Latin letter or space.


1 Answers

In java you must double escape the predefined classes.

String rgx = "[^\\s,]";
like image 108
roblovelock Avatar answered Oct 22 '22 14:10

roblovelock