in reply to Question on pattern matching

Check for a non-digit:

if ($my_dat =~ /[^0-9]/) { # $my_dat contains a non-digit. ... }

or check for all digits:

if ($my_dat =~ /^[0-9]+$/) { # $my_dat only contains digits. ... }

Update: If you need to make sure the string is only two characters long:

if ($my_dat =~ /^[0-9]{2}$/) { # $my_dat only contains digits. # $my_dat is exactly two characters long. ... }

Replies are listed 'Best First'.
Re^2: Question on pattern matching
by ikegami (Patriarch) on Jun 16, 2005 at 21:05 UTC

    This also works:

    if ($my_dat eq sprintf('%02d', do { no warnings; 0+$my_dat })) { # $my_dat only contains digits. # $my_dat is exactly two characters long. ... }