190

I was wondering If I could get a regular expression which will match a string that only has alphabetic characters, and that alone.

Zeeshan Hassan Memon
  • 7,773
  • 3
  • 40
  • 53
Steffan Harris
  • 8,706
  • 29
  • 69
  • 98

5 Answers5

234

You may use any of these 2 variants:

/^[A-Z]+$/i
/^[A-Za-z]+$/

to match an input string of ASCII alphabets.

  • [A-Za-z] will match all the alphabets (both lowercase and uppercase).
  • ^ and $ will make sure that nothing but these alphabets will be matched.

Code:

preg_match('/^[A-Z]+$/i', "abcAbc^Xyz", $m);
var_dump($m);

Output:

array(0) {
}

Test case is for OP's comment that he wants to match only if there are 1 or more alphabets present in the input. As you can see in the test case that matches failed because there was ^ in the input string abcAbc^Xyz.

Note: Please note that the above answer only matches ASCII alphabets and doesn't match Unicode characters. If you want to match Unicode letters then use:

/^\p{L}+$/u

Here, \p{L} matches any kind of letter from any language

Shofol
  • 513
  • 1
  • 8
  • 23
anubhava
  • 713,503
  • 59
  • 514
  • 593
64

If you need to include non-ASCII alphabetic characters, and if your regex flavor supports Unicode, then

\A\pL+\z

would be the correct regex.

Some regex engines don't support this Unicode syntax but allow the \w alphanumeric shorthand to also match non-ASCII characters. In that case, you can get all alphabetics by subtracting digits and underscores from \w like this:

\A[^\W\d_]+\z

\A matches at the start of the string, \z at the end of the string (^ and $ also match at the start/end of lines in some languages like Ruby, or if certain regex options are set).

Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
23

This will match one or more alphabetical characters:

/^[a-z]+$/

You can make it case insensitive using:

/^[a-z]+$/i

or:

/^[a-zA-Z]+$/
stevecomrie
  • 2,375
  • 18
  • 28
15

In Ruby and other languages that support POSIX character classes in bracket expressions, you can do simply:

/\A[[:alpha:]]+\z/i

That will match alpha-chars in all Unicode alphabet languages. Easy peasy.

More info: http://en.wikipedia.org/wiki/Regular_expression#Character_classes http://ruby-doc.org/core-2.0/Regexp.html

jshkol
  • 1,697
  • 14
  • 19
  • 1
    And to get everything but those characters (which wasn't documented) use `[^[:alpha]]`. – spyle Sep 25 '14 at 18:07
6

[a-zA-Z] should do that just fine.

You can reference the cheat sheet.

Frazell Thomas
  • 5,941
  • 1
  • 19
  • 21