Home: Perl Programming Help: Advanced:
Help In Checking content........



yotam_k
Deleted

Jan 17, 2001, 2:46 PM


Views: 5058
Help In Checking content........

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


Views: 5053
Re: Help In Checking content........

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

Sleuth



yotam_k
Deleted

Jan 26, 2001, 8:42 AM


Views: 5026
Re: Help In Checking content........

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



modified
Deleted

Jan 26, 2001, 1:43 PM


Views: 5022
Re: Help In Checking content........


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



Jasmine
Administrator / Moderator

Jan 26, 2001, 7:41 PM


Views: 5019
Re: Help In Checking content........

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


Views: 5015
Re: Help In Checking content........

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


Views: 5013
Re: Help In Checking content........

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


Views: 4964
Re: Help In Checking content........

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


Views: 4961
Re: Help In Checking content........

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"@_,"