Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting illegal character range in regex :java

I have a simple regex pattern that verifies names. But When I run it I get illegal character range error. I thought by escaping "\s" it will allow a space but the compiler is still complaining.

public boolean verifyName(String name) {
    String namePattern = "^[\\p{L}]++(?:[',-\\s][\\p{L}]++)*+\\.?$";
    return name.matches(namePattern);
}

and this is the error that i think shouldn't be occurring since a name might contain anny of these [',-\\s]

so where am i not understanding?

like image 967
Obby Avatar asked Sep 14 '25 03:09

Obby


1 Answers

You can't have a range "from , to whitespace". Perhaps you meant to escape -?

\s is not a space, it's [ \t\r\n\v\f] (space, tab, carriage return, newline, vertical tab or a form feed).

Things that will work:

"[ ',-]"

"[',\\- ]"
like image 171
Pavel Anossov Avatar answered Sep 16 '25 17:09

Pavel Anossov