I was wondering If I could get a regular expression which will match a string that only has alphabetic characters, and that alone.
-
3Something like /^[a-zA-Z]+$/ should work. – Julian Go May 20 '11 at 04:56
-
11Is `à` an alphabetic character according to your definition? What language are you using? – Tim Pietzcker May 20 '11 at 06:18
-
2should an empty string match? – ysth May 20 '11 at 06:30
-
nope an empty string should not match – Steffan Harris May 20 '11 at 07:04
-
1One important note: you didn't refered a language or tool where you wwant to use the regex you're asking. Altough the principles of the regexes are the same universally, the syntax is not equally everywhere. You should refer where you want to use it. – sergiol May 20 '11 at 10:01
-
Define what do you mean by 'alphabetic character'. A string いただきます contains only alphabetic characters. Should it pass ? – koryakinp Nov 01 '17 at 01:55
6 Answers
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
-
3
-
11Nope `[A-z]` is wrong as it matches many other characters not just `[A-Za-z]` – anubhava Nov 04 '20 at 06:10
-
Hmm interesting I did not know that. Is that other languages? Thanks :) – chris c Nov 04 '20 at 22:01
-
1If you look up an ASCII table you will see the characters between Z and a – Alexander Wu Feb 14 '21 at 03:21
-
2I knew that `\p{L}` [works in Java](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html) but I didn't know that [it's universal](https://regex101.com/r/SlQvEs/1) until I came across this answer. Thanks for sharing this knowledge. – Arvind Kumar Avinash Apr 13 '21 at 10:21
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).

- 328,213
- 58
- 503
- 561
-
55
-
9+1, same as above. english is not the only alphabet and many people write their name using non-ascii characters to express it correctly. – Ben Barkay Apr 07 '13 at 07:36
This will match one or more alphabetical characters:
/^[a-z]+$/
You can make it case insensitive using:
/^[a-z]+$/i
or:
/^[a-zA-Z]+$/

- 2,423
- 20
- 28
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

- 1,777
- 14
- 19
-
1And to get everything but those characters (which wasn't documented) use `[^[:alpha]]`. – spyle Sep 25 '14 at 18:07
[a-zA-Z]
should do that just fine.
You can reference the cheat sheet.

- 6,031
- 1
- 20
- 21
-
5yes but I would also if my string contained a non word character it would still match – Steffan Harris May 20 '11 at 05:16
For me this worked: Press ctrl + H in Notepad++ and find
([a-z ]+)
replace
"\1"
Turn on Regular Expression

- 848
- 7
- 12