Home: Perl Programming Help: Regular Expressions:
testing number at start of string



simo_mon
New User

Jun 2, 2009, 7:49 PM


Views: 5815
testing number at start of string

hi there,

i have a string and i am trying to test if there is a number at the
start


Code
 
my $string = "12 asdkjh";

if( $string =~ m/^d+/)
{
print " starts with a number";


}


when i run that i get nothing, but when i run this


Code
 
if ( $string =~ m/^[0-9]/)
{
print " got a number at the start ";

}



i get -> got a number at the start...

am i doing something wrong here ....

because ^ = start of string
d = digit
d+ = 1 or many times

???

confused


KevinR
Veteran


Jun 2, 2009, 8:42 PM


Views: 5814
Re: [simo_mon] testing number at start of string

A beginners mistake. You forgot the backslash.

d <-- just a 'd'
\d <--- a digit 0-9
\d+ <-- more than one digit
-------------------------------------------------


simo_mon
New User

Jun 2, 2009, 9:27 PM


Views: 5812
Re: [KevinR] testing number at start of string

lol thanks

embarrassing :)


1arryb
User

Jun 3, 2009, 9:49 AM


Views: 5802
Re: [KevinR] testing number at start of string

Kevin,

Minor correction:

Quote
\d+ <-- more than one digit

\d+ <-- one or more digits.

Larry


KevinR
Veteran


Jun 3, 2009, 2:21 PM


Views: 5792
Re: [1arryb] testing number at start of string


In Reply To
Kevin,

Minor correction:

Quote
\d+ <-- more than one digit

\d+ <-- one or more digits.

Larry


ahhh... good catch Larry, thanks. Blush
-------------------------------------------------