tags:

views:

284

answers:

5

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 ";    
 }
+7  A: 

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
ghostdog74
The [a-zA-Z] takes a particularly American view to letters. It's a bigger world out there. The POSIX character class would be better, but matching the Unicode Letter property is the same thing and easier to type. :)
brian d foy
`/[a-zA-z]/` <-- the second z is probably meant to be uppercased (`/[a-zA-Z]/`), or the first `a-z` should be omitted (see my example)
Dan Beam
+6  A: 

If you want to match Unicode characters rather than just ASCII ones, try this:

#!/usr/bin/perl

while (<>) {
  if (/[\p{L}]+/) {
    print "letters\n";
  } else {
    print "no letters\n";
  }
}
Makis
+3  A: 

If you're looking for any kind of letter from any language, you should go with

\p{L}

Take a look on this full reference: Unicode Character Properties

Rubens Farias
A: 

Using /[A-Za-z]/ is a US-centric way to do it. To accept any letter, use one of

  • /[[:alpha:]]/
  • /\p{L}/
  • /[^\W\d_]/

The third one employs a double-negative: not not-a-letter, not a digit, and not an underscore.

Whichever you choose, those who maintain your code will certainly appreciate it if you stick with one consistently!

Greg Bacon
A: 

If you're looking to detect whether something looks like a number for the purposes of manipulating it in Perl, you'll want Scalar::Util::looks_like_number (core since perl 5.7.3). From perlapi:

looks_like_number

Test if the content of an SV looks like a number (or is a number). Inf and Infinity are treated as numbers (so will not issue a non-numeric warning), even if your atof() doesn't grok them.

Anonymous