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?
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.
\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.
In java you must double escape the predefined classes.
String rgx = "[^\\s,]";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With