Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: String pattern : how to specify regex for all alpha characters with special characters

Tags:

java

string

regex

I want to be sure that a String contains only alpha characters (with special character like "é", "è", "ç", "Ç", "ï", etc etc.).

I did that, but with special characters returns false...

if (myString.matches("^[a-zA-Z]+$")) {
    return true;
}

Thanks guys!

like image 210
anthony Avatar asked Dec 19 '15 09:12

anthony


1 Answers

You can use Unicode Category: \\p{L} or \\P{Letter} to match any kind of letter from any language.

if (myString.matches("\\p{L}+")) {
    return true;
}

BTW, String.matches try to match entire string, so ^, $ anchors are not necessary.

like image 142
falsetru Avatar answered Sep 24 '22 02:09

falsetru