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: Regular Expressions:
Newbie regex perplexity...

 



parmenides
Deleted

Nov 25, 2000, 8:23 AM

Post #1 of 3 (1315 views)
Newbie regex perplexity... Can't Post

I'm putting together a form validation/email CGI script and am completely new to Perl. The CGI part is actually working quite well, but I can NOT seem to get the hang of Perl's flavor of regex.

For form validation, I want to allow the user to enter the following characters in a telephone field:

0123456789-.() and spaces

How do I put togther the regex for this? Checking for numbers works great:

if ($value and $value =~ /\D/)...

But EVERYTHING I've tried to allow typing the other characters causes the check to always fail. Can someone help me get my bearings here? Thanks!


japhy
Enthusiast / Moderator

Nov 25, 2000, 8:36 AM

Post #2 of 3 (1315 views)
Re: Newbie regex perplexity... [In reply to] Can't Post

You want to check to make sure they used only digits, hyphens, periods, parentheses, and spaces.

You want to use a "negated character class". Negated just means "take the opposite of..."

To match digits, hyphens, etc., you'd use [\d\s().-] which match either a digit, a whitespace, an open paren, a close paren, a period, or a hyphen. To get the opposite of that, you include a ^ as the first character of the class: [^\d\s().-]

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($value =~ /[^\d\s().-]/) {
print "bad value: '$value'\n";
}
</pre><HR></BLOCKQUOTE>

In fact, you could even tell the user what character was bad:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($value =~ /([^\d\s().-])/) {
print "bad character in '$value': '$1'\n";
}
</pre><HR></BLOCKQUOTE>

The parens around the character class will store whatever matches into $1 (or $2, or $3, depending on how many sets of parens you've used). If you want to tell the user about ALL the bad characters they put in their string, you can do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if (@bad = $value =~ /([^\d\s().-])/g) {
print "bad characters in '$value': @bad\n";
}
</pre><HR></BLOCKQUOTE>

The g modifier to the regex makes it match as many times as possible, and since we're assigning the result of the pattern match to an array, the array will be filled with all the matches.

For more enlightenment, read the 'perlre' documentation.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



parmenides
Deleted

Nov 25, 2000, 9:19 AM

Post #3 of 3 (1315 views)
Re: Newbie regex perplexity... [In reply to] Can't Post

Thanks, japhy!

/[^\d\s().-]/ did the trick! I had tried something very similar to this, but "similar" just doesn't work for regex. Think I may have tried to escape the "-", since it's used for ranges ("[0-9]").

Also, sorry this message showed up twice! Apparently, I was doing an edit while you were moving it to this forum. My apologies...

 
 


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

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