Regex to match only letters

Nike Source

How can I write a regex that matches only letters?



answered 8 years ago Gumbo #1

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.

answered 8 years ago RobV #2

\p{L} matches anything that is a Unicode letter if you're interested in alphabets beyond the Latin one

answered 8 years ago Scott Radcliff #3


Super simple example. Regular expressions are extremely easy to find online.

answered 8 years ago Molske #4

Depending on your meaning of "character":

[A-Za-z] - all letters (uppercase and lowercase)

[^0-9] - all non-digit characters

answered 5 years ago Rohit Dubey #5

For PHP, following will work fine


answered 5 years ago Sławomir Lenart #6

If you mean any letters in any character encoding, then a good approach might be to delete non-letters like spaces \s, digits \d, and other special characters like:

[[email protected]#\$%\^&\*\(\)\[\]:;'",\. ...more special chars... ]

Or use negation of above negation to directly describe any letters:

\S \D and [^  ..special chars..]


  • Works with all regex flavors.
  • Easy to write, sometimes save lots of time.


  • Long, sometimes not perfect, but character encoding can be broken as well.

answered 4 years ago Agaspher #7

Just use \w or [:alpha:]. It is an escape sequences which matches only symbols which might appear in words.

answered 4 years ago Snm Maurya #8

pattern = /[a-zA-Z]/

puts "[a-zA-Z]: #{pattern.match("mine blossom")}" OK

puts "[a-zA-Z]: #{pattern.match("456")}"

puts "[a-zA-Z]: #{pattern.match("")}"

puts "[a-zA-Z]: #{pattern.match("#$%^&*")}"

puts "[a-zA-Z]: #{pattern.match("#$%^&*A")}" OK

answered 4 years ago Motlab #9

You can try this regular expression : [^\W\d_] or [a-zA-Z].

answered 4 years ago blue_note #10

The closest option available is


which matches a sequence of uppercase and lowercase letters. However, it is not supported by all editors/languages, so it is probably safer to use


as other users suggest

answered 2 years ago Scott #11

You would use


[]--checks for any characters between given inputs

a-z---covers the entire alphabet

g-----globally throughout the whole string

i-----getting upper and lowercase

answered 2 years ago Fikreselam Elala #12

Pattern pattern = Pattern.compile("^[a-zA-Z]+$");

if (pattern.matcher("a").find()) { something ......

answered 2 years ago Yogesh Chauhan #13

Regular expression which few people has written as "/^[a-zA-Z]$/i" is not correct because at the last they have mentioned /i which is for case insensitive and after matching for first time it will return back. Instead of /i just use /g which is for global and you also do not have any need to put ^ $ for starting and ending.

  1. [a-z_]+ match a single character present in the list below
  2. Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed
  3. a-z a single character in the range between a and z (case sensitive)
  4. A-Z a single character in the range between A and Z (case sensitive)
  5. g modifier: global. All matches (don't return on first match)

answered 2 years ago Udeshika Sewwandi #14


String s= "abcdef";

     System.out.println("string only contains letters");

answered 1 year ago Tomáš Nedělka #15

Use character groups


Matches any character except digits 0-9


See example here

comments powered by Disqus