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: Advanced:
Help In Checking content........

 



yotam_k
Deleted

Jan 17, 2001, 2:46 PM

Post #1 of 9 (2072 views)
Help In Checking content........ Can't Post

Could SomeOne FeeD me baCk In How verifing If "$mystring" Contains Numbers anD verifing If "$mystring" Contains Charcters?

thank you all !



sleuth
Enthusiast

Jan 17, 2001, 9:13 PM

Post #2 of 9 (2067 views)
Re: Help In Checking content........ [In reply to] Can't Post

 
If ($mystring =~ /[A-Za-z0-9]/){
print "Has Number And Letters";
}

Sleuth



yotam_k
Deleted

Jan 26, 2001, 8:42 AM

Post #3 of 9 (2040 views)
Re: Help In Checking content........ [In reply to] Can't Post

And If I want To verify if it just contains numbers and numbers only?



modified
Deleted

Jan 26, 2001, 1:43 PM

Post #4 of 9 (2036 views)
Re: Help In Checking content........ [In reply to] Can't Post


Code
if ($mystring =~ /[0-9]/) { 
print "String has only numbers!";
}



Jasmine
Administrator / Moderator

Jan 26, 2001, 7:41 PM

Post #5 of 9 (2033 views)
Re: Help In Checking content........ [In reply to] Can't Post

I beg to differ a little :)


Code
if ($mystring =~ /[0-9]/) { 
print "String has only numbers!";
}

means that $mystring contains numbers, not that it contains only numbers.

To check to see if a string is all numbers, use:


Code
if ($mystring =~ /\D/) { 
print "String has non-numbers!";
}
else {
print "String has numbers only!";
}



modified
Deleted

Jan 27, 2001, 7:15 AM

Post #6 of 9 (2029 views)
Re: Help In Checking content........ [In reply to] Can't Post

Err, sorry, I don't understand regular experisons that well. :) So if he wanted only numbers without the check for letters, it would be....


Code
if ($mystring !~ /\D/) { 
print "Mystring has only numbers!";
}

Right?



Jasmine
Administrator / Moderator

Jan 27, 2001, 9:02 AM

Post #7 of 9 (2027 views)
Re: Help In Checking content........ [In reply to] Can't Post

That is correct -- the !~ negates the match for \D (non-digits). Alternatively, if you wanted to check for non alphabetic characters, you would use \W (non-letters) instead.



carpedout
stranger

Oct 9, 2001, 2:38 AM

Post #8 of 9 (1978 views)
Re: Help In Checking content........ [In reply to] Can't Post

I'm pushing it - but I'm lost with regex's and I'd like to know how to limit form input to be ONLY numbers with or without a decimal point and NOT allow a dollar symbol... pleeease...

$mystring =~ /[0-9]./ ???



mhx
Enthusiast

Oct 9, 2001, 7:09 AM

Post #9 of 9 (1975 views)
Re: Help In Checking content........ [In reply to] Can't Post

You're looking for either

Code
$mystring =~ /^[0-9.]*$/;

or

Code
$mystring !~ /[^0-9.]/;

The first one checks if there are only digits or the decimal point between the beginning (^) and the end ($) of the string. The second one checks if the string does _not_ (!~) contain any character that is not ([^) a digit or a decimal point.
If you additionally would want to disallow empty strings and multiple decimal points and allow leading or trailing whitespace, you should use:

Code
$mystring =~ /^\s*(\d+(?:\.\d*)?)\s*$/;

This will additionally put the number itself into the variable $1.
You could also think of allowing only a certain number of digits before and after the decimal point. Say you would want to have 1 to 5 digits before and 0 to 2 digits after the decimal point, try:

Code
$mystring =~ /^\s*(\d{1,5}(?:\.\d{0,2})?)\s*$/;

This again will leave the number in $1.
Now, this has become a quite complex regex, so I hope you're able to follow. Perhaps you can reuse any of the above regexes for your purposes.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


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

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