
japhy
Enthusiast
Feb 18, 2000, 5:47 AM
Post #5 of 6
(478 views)
|
kencl, perlkid -- you're missing the regular expression delimiters (the /'s). To find out if a string is ONLY letters, I'd suggest doing: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> if ($string !~ /[^A-Za-z]/) { # $string does not have any non-letters } </pre><HR></BLOCKQUOTE> I suggest that, INSTEAD OF <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> if ($string =~ /^[A-Za-z]+$/) { # $string has all letters } </pre><HR></BLOCKQUOTE> for TWO reasons. First, the one I suggest is an application of logic (if $string is all letters, then it CAN'T have non-letters). Second, is because the $ anchor in a regex can match directly before a newline (which is NOT a letter). You can get around that by saying: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> if ($string =~ /^[A-Za-z]+(?!\n)$/) { # ... } </pre><HR></BLOCKQUOTE> Anyway. To match a string as all numbers, I'd suggest: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> if ($string !~ /\D/) { # \d is the [0-9] char class # \D is non-digits } </pre><HR></BLOCKQUOTE> Thus endeth the lesson. [This message has been edited by japhy (edited 02-18-2000).]
|