How can I check if a Perl string contains letters?

perl, regex

Solution

try this:

/[a-zA-Z]/

or

/[[:alpha:]]/

otherwise, you should give examples of the strings you want to match.

also read `perldoc perlrequick`

Edit: @OP, you have provided example string, but i am not really sure what you want to do with it. so i am assuming you want to check whether a word is all letters, all numbers or something else. here's something to start with. All from perldoc perlrequick (and perlretut) so please read them.

sub check{
    my $str = shift;
    if ($str =~ /^[a-zA-Z]+$/){
        return $str." all letters";
    }
    if ($str =~ /^[0-9]+$/){
        return $str." all numbers";
    }else{
        return $str." a mix of numbers/letters/others";
    }
}

$string = "99932";
print check ($string)."\n";
$string = "abcXXX";
print check ($string)."\n";
$string = "9abd99_32";
print check ($string)."\n";

output

$ perl perl.pl
99932 all numbers
abcXXX all letters
9abd99_32 a mix of numbers/letters/others

Problem

In Perl, what regex should I use to find if a string of characters has letters or not? Example of a string used: `Thu Jan 1 05:30:00 1970` Would this be fine? ``` if ($l =~ /[a-zA-Z]/) { print "string "; } else { print "number "; } ```

Original source