CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Regular Expressions:
Newbie regex perplexity...



Nov 25, 2000, 8:23 AM

Post #1 of 3 (4143 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!

Enthusiast / Moderator

Nov 25, 2000, 8:36 AM

Post #2 of 3 (4143 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";

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";

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";

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


Nov 25, 2000, 9:19 AM

Post #3 of 3 (4143 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