Regex to match only letters

Nike picture Nike · Sep 1, 2010 · Viewed 788.1k times · Source

How can I write a regex that matches only letters?

Answer

Gumbo picture Gumbo · Sep 1, 2010

Use a character set: [a-zA-Z] matches one letter from A–Z in lowercase and uppercase. [a-zA-Z]+ matches one or more letters and ^[a-zA-Z]+$ matches only strings that consist of one or more letters only (^ and $ mark the begin and end of a string respectively).

If you want to match other letters than A–Z, you can either add them to the character set: [a-zA-ZäöüßÄÖÜ]. Or you use predefined character classes like the Unicode character property class \p{L} that describes the Unicode characters that are letters.