I would really like to use \w but it also matches underscores so I'm going with [A-Za-z] which feels unnecessarily verbose and America centric. Is there a better way to do this? Something like [\w^_] (I doubt I got that syntax right)?
-
4`[A-Za-z]` isn't America-centric, it's ASCII-centric. – CanSpice Sep 20 '10 at 19:09
-
@CanSpice, I'd say it's Latin-centric, as ASCII is only uses the latin alphabet. – Axeman Sep 20 '10 at 19:53
-
1@Axeman, Sources? AFAIK many Latin-derived languages have stressed vowels and letters. [A-Za-z] won't match neither Español nor Française. Even pure latin has the Æ glyph that is not inside ASCII. Which Latin are you talking about? – Kyordhel Nov 27 '17 at 19:28
-
1@Gzork, *Classical* 23 character Latin alphabet + the Medieval J, U, & W – Axeman Nov 28 '17 at 22:40
7 Answers
Perhaps you mean /[[:alpha:]]/
? See perlre for the discussion of POSIX character classes.

- 14,573
- 6
- 35
- 54
-
1I still don't think this take the international characters into consideration, since `:alpha:` is still `a-zA-Z` – vol7ron Sep 20 '10 at 18:32
-
I was wrong; the perldocs should be updated. However it does include extra punctuations: `ʹʺʻˍˎˏːˑˬˮ̀́` – vol7ron Sep 20 '10 at 23:48
-
@vol7ron: The extra punctuation spills beyond the comment into your username. (Firefox 3.6.10 on Windows XP) – Jon Purdy Sep 21 '10 at 06:07
-
@Jon Purdy: ahh, I suspect that's a side effect of having unicode characters in output - another reason not to use :alpha: – vol7ron Sep 30 '10 at 03:37
Just use \p{L}
which means "any Unicode letter" and works in Perl (/\p{L}/
). You probably need to use utf8;
.

- 328,213
- 58
- 503
- 561
Matching international (i.e non-ASCII) characters is kind of tough, and could depend on a lot of things. Check out this example:
#!perl -w
use strict;
use utf8;
my $string = "ä";
print "matched :alpha:\n" if $string =~ /[[:alpha:]]/;
print "matched ^\\W0-9_\n" if $string =~ /[^\W0-9_]/;
print "matched [a-zA-Z]\n" if $string =~ /[a-zA-Z]/;
print "matched [a-z]i\n" if $string =~ /[a-z]/i;
print "matched [A-z]\n" if $string =~ /[A-z]/;
For me this results in
matched :alpha:
If you remove the use utf8
then none of the regular expressions match.
Looking at this very relevant question, it looks like you probably want to use utf8
and check out Unicode::Semantics.
Of course, if you're using straight ASCII characters than any of the aforementioned regular expressions will work.
-
You have to be careful with \w and \W in Perl. It's a bit of a moving target lately, so I wouldn't depend on its definition. – brian d foy Sep 20 '10 at 21:19
-
What your editor is displaying as 'ä' may not be a letter in Perl. 'ä' = "\xE4" is not defined in the ASCII character set, by default Perl will use ASCII character classes for string that are not encoded as UTF8. Adding `use utf8;` will treat your source code as UTF8 and make "ä" a Unicode string. In the Unicode character set 'ä' is a alpha character. Compare `$string = "ä";` vs `$string = decode("iso-8859-1", "ä");` without `use utf8;`. In the first case Perl will see binary data, in the second the binary data is being interpreted as Latin-1 and converted to UTF-8. – Ven'Tatsu Sep 21 '10 at 20:29
You could use /[a-z]/i
or /[[:alpha:]]/
just as well. In fact, \w
includes numbers so that won't even work.

- 13,370
- 2
- 32
- 51
A few options:
1. /[a-z]/i # case insensitive
2. /[A-Z]/i # case insensitive
3. /[A-z]/ # explicit range listing (capital 'A' to lowercase 'z')
4. /[[:alpha:]]/ # POSIX alpha character class
I recommend using either the case-insensitive, or the true way /[a-zA-z]/
, unless you have a certain language preference in mind.
Note:
- Number 3 requires the capital 'A' first and then lowercase 'z' because of the order of the ASCII values; it does not work if you do the reverse:
a-Z
. Also: this method would fail the no-underscore criteria, since it includes [ \ ] ^ _ ` . - Number 4 will match on those additional language characters, but it also matches on:
ʹʺʻˍˎˏːˑˬˮ̀́
(plus many others)

- 40,809
- 21
- 119
- 172
-
-
You are correct there's a few brackets and extra characters, which would break the no-underscore criteria. I've modified the answer. @flies, yes but I was incorrect. Really the first two are the same method, just showing you can use different inputs. – vol7ron Sep 20 '10 at 23:22
you're looking for internationalization in your regex? then you'll need to do something like this guy did: JavaScript validation issue with international characters
explicitly match on all of the moon language letters :)

- 1
- 1

- 4,437
- 2
- 29
- 28
-
I don't see any moon letters in that regex, but it might just be because I'm reading it in the daylight. – Matt K Sep 20 '10 at 18:46
-