anthony
anthony

Reputation: 7733

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

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!

Upvotes: 7

Views: 1289

Answers (1)

falsetru
falsetru

Reputation: 369054

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.

Upvotes: 7

Related Questions