6.2. Matching LettersProblemYou want to see whether a value only consists of alphabetic characters. SolutionThe obvious character class for matching regular letters isn't good enough in the general case: if ($var =~ /^[A-Za-z]+$/) { # it is purely alphabetic }
That's because it doesn't respect the user's locale settings. If you need to match letters with diacritics as well, use locale; if ($var =~ /^[^\W\d_]+$/) { print "var is purely alphabetic\n"; } Discussion
Perl can't directly express "something alphabetic" independent of locale, so we have to be more clever. The Here's how you'd use this in a program: use locale; use POSIX 'locale_h'; # the following locale string might be different on your system unless (setlocale(LC_ALL, "fr_CA.ISO8859-1")) { die "couldn't set locale to French Canadian\n"; } while (<DATA>) { chomp; if (/^[^\W\d_]+$/) { print "$_: alphabetic\n"; } else { print "$_: line noise\n"; } } __END__ silly façade coöperate niño Renée Molière hæmoglobin naïve tschüß random!stuff#here See AlsoThe treatment of locales in Perl in perllocale (1); your system's locale (3) manpage; we discuss locales in greater depth in Recipe 6.12 ; the "Perl and the POSIX Locale" section of Chapter 7 of Mastering Regular Expressions |
|