Getting illegal character range in regex :java

对着背影说爱祢 提交于 2019-12-02 09:19:26

问题


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?


回答1:


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:

"[ ',-]"

"[',\\- ]"


来源:https://stackoverflow.com/questions/16117762/getting-illegal-character-range-in-regex-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!