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: Beginner:
Matching rules in Perl

 



g0blu322
New User

Nov 28, 2012, 3:40 PM

Post #1 of 3 (873 views)
Matching rules in Perl Can't Post

So I'm in the beginning stages of learning perl. I was giyen a set of different rules that a string could have. But, my output is either the first rule or "No conditions are met(the last one) The rules are self-explanatory in my print statements. Here's what I typed up:

print "Input a string";
$string = <>;
if($string == /[3579][02468]/)
{
print "$string is an odd digit followed by an even digit";
}
elsif($string == /[a-zA-Z][^a-zA-Z][0-9]/)
{
print "$string is a letter followed by a non-letter followed by a number";
}
elsif($string == /\b[A-Z]/)
{
print "$string is a word that starts with an uppercase letter";
}
elsif($string == /\b[Yy][Ee][Ss]\b/)
{
print "$string is the word yes in any combination of uppercase and lowercase letters";
}
elsif($string == /(\bthe\b)+/)
{
print "$string is the word with one or more times of 'the'";
}
elsif($string == /\d\d?\.\d\d?\.\d\d/ )
{
print"$string is a date in the form of one or two digits, a dot, one or 2 digits, a dot, two digits";
}
elsif($string == /[\!:;"',\.\?`]/)
{
print "$string is a puncuation mark";
}
else
{
print "No conditions are met for $string";
}


rovf
Veteran

Nov 29, 2012, 3:42 AM

Post #2 of 3 (864 views)
Re: [g0blu322] Matching rules in Perl [In reply to] Can't Post

I bet that if you enter a string consisting of the single letter R, one rule (but not the first one) will match.


Laurent_R
Enthusiast / Moderator

Nov 29, 2012, 4:08 AM

Post #3 of 3 (863 views)
Re: [g0blu322] Matching rules in Perl [In reply to] Can't Post

Hum, a couple of points.


Code
if($string == /[3579][02468]/) 
{
print "$string is an odd digit followed by an even digit";
}


1. If your string is 12, it will not match because you don't have 1 in your first character class;

2. If you get a match, this does not mean at all that "$string is an odd digit followed by an even digit", it means that "$string contains, somewhere, an odd digit (but not 1) followed by an even digit", so that 'foo36bar}%"&\' will match your regex. Of course, the same remark can be made about the other matches in you code.

Otherwise, please tell us what strings fall into the default "no conditions" message, as I am fairly sure that 'A_7', for excample, should match your second regex (and not the first one).

 
 


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

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