
mhx
Enthusiast
Mar 26, 2002, 11:11 PM
Post #2 of 10
(21320 views)
|
Re: [Jester_48] New to Perl Need Help
[In reply to]
|
Can't Post
|
|
this should match a name in the formats J, Joe, John, John Paul or John-Paul with a minimum of 1 character entered and a maximum of 30 if (param("name") !~ /([A-Z][a-z]*-? ?[A-Z]*[a-z]*){1,30}/) { then output error msg } That's not what it does. You can't (at least I believe you can't ;) accomplish the task of checking the total number of characters and matching against your pattern in a single regex. So, that's what I would do: [perl] #!/usr/bin/perl -w use strict; my $name = '(?:[A-Z][a-z]*)'; my $regex = qr/^$name(?:[\s-]$name)?$/; while( <DATA> ) { chomp; s/^\s+//; s/\s+$//; # remove leading/trailing blanks print "'$_' => FAIL\n" if length > 30 or $_ !~ $regex; } __DATA__ Thisnamematches Butitisfartoooooolong J Joe John John Paul John-Paul JASON Judy J J [/perl] where the essential part is the regular expression that only tests if the name itself is valid, while the length check is done with a separate length call. The check for a minimum length of 1 is implicitly included in the regex.
this is supposed to match an email address, I am trying to allow for a 4 character name for the extension (ie new .name addresses) if (param("email") !~ /^\w*\@\w*\.[A-Za-z]{3,4}$/) { then output error msg } Don't try to invent your own email-matching-regex. The only true regex that will check an email-address for validity is over 6000 bytes and looks more or less like this:
[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\ xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xf f\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\x ff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n\015 "]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\ ... so I guess you won't hack it yourself. Fortunately, it is available through the [url=http://search.cpan.org/search?dist=Email-Valid]Email::Valid module with a nice interface. -- mhx
At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."-- Frodo
|