CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



simo_mon
New User

Jun 2, 2009, 7:49 PM

Post #1 of 5 (2802 views)
testing number at start of string Can't Post

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

Post #2 of 5 (2801 views)
Re: [simo_mon] testing number at start of string [In reply to] Can't Post

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

Post #3 of 5 (2799 views)
Re: [KevinR] testing number at start of string [In reply to] Can't Post

lol thanks

embarrassing :)


1arryb
User

Jun 3, 2009, 9:49 AM

Post #4 of 5 (2789 views)
Re: [KevinR] testing number at start of string [In reply to] Can't Post

Kevin,

Minor correction:

Quote
\d+ <-- more than one digit

\d+ <-- one or more digits.

Larry


KevinR
Veteran


Jun 3, 2009, 2:21 PM

Post #5 of 5 (2779 views)
Re: [1arryb] testing number at start of string [In reply to] Can't Post


In Reply To
Kevin,

Minor correction:

Quote
\d+ <-- more than one digit

\d+ <-- one or more digits.

Larry


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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives