Mar 23, 2004 #1 Oostwijk Technical User Oct 19, 2003 82 NL How to check if the first character in a string is a number ?
Mar 23, 2004 #2 cgimonk Programmer Mar 14, 2004 18 US Code: if (/^\d/) { .... } Will work I /think/. I could be wrong. Upvote 0 Downvote
Mar 23, 2004 Thread starter #3 Oostwijk Technical User Oct 19, 2003 82 NL where in the code can I place the string ? Upvote 0 Downvote
Mar 23, 2004 #4 ishnid Programmer Aug 29, 2003 1,422 IE cgimonk's code assumes that your string is $_. To use another: Code: if ($string =~ /^\d/) { .... } Upvote 0 Downvote
Mar 23, 2004 Thread starter #5 Oostwijk Technical User Oct 19, 2003 82 NL It doesn't seem to work, anyone got another suggestion ? Upvote 0 Downvote
Mar 23, 2004 #6 siberian Programmer Sep 27, 2003 1,295 US Worked for me, maybe your string is really NOT a number? Code: $string = "1234"; if ($string =~ /^\d/) { print "I am a number"; } else { print "I am NOT a number";} Upvote 0 Downvote
Worked for me, maybe your string is really NOT a number? Code: $string = "1234"; if ($string =~ /^\d/) { print "I am a number"; } else { print "I am NOT a number";}
Mar 23, 2004 1 #7 mikevh Programmer Apr 23, 2001 1,033 US It should work, if the first char is really a digit. Is there whitespace before the number? Then you might try Code: if ($string =~ /^\s*\d/) { .... } This allows for zero or more whitepace chars before the digit, so will work if there's whitespace before or not. Upvote 0 Downvote
It should work, if the first char is really a digit. Is there whitespace before the number? Then you might try Code: if ($string =~ /^\s*\d/) { .... } This allows for zero or more whitepace chars before the digit, so will work if there's whitespace before or not.