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:
Regular expression validations

 



Palpatine43
New User

May 18, 2013, 4:03 PM

Post #1 of 3 (442 views)
Regular expression validations Can't Post

I am trying to learn some regular expressions to validate different inputs. Can someone help me out with these?

Provide a perl regular expression that can be used to validate each of the following pattern descriptions:
• a 3 digit area code
• a 3 digit area code that must be enclosed in parenthesis
• a 3 digit area code that may be enclosed in parenthesis
• a 9 digit social security number
• a 9 digit social security number that must include dashes after the 3rd and 5th digit
• a 9 digit social security number that might include dashes after the 3rd and 5th digit

Thank you!


BillKSmith
Veteran

May 18, 2013, 4:17 PM

Post #2 of 3 (430 views)
Re: [Palpatine43] Regular expression validations [In reply to] Can't Post

Most are already available in the module Regexp::Common.
Good Luck,
Bill


hwnd
User

May 18, 2013, 6:27 PM

Post #3 of 3 (415 views)
Re: [Palpatine43] Regular expression validations [In reply to] Can't Post

A good start would be to check out tutorials you can find online, also Perl Regular Expressions Cookbook.


Code
my @phone = ('1234567890', '123-456-7890', '123.456.7890', 
'123 456 7890', '(123) 456-7890');

my @social = ('111111111', '111-11-1111', '111 11 1111');

for (@phone) {
if ( /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/ ) {
print "match\n";
}
}

for (@social) {
if ( /^?([0-9]{3})?[- ]?([0-9]{2})[- ]?([0-9]{4})$/ ) {
print "match\n";
}
}



Explanation of the regular expression:


Code
^                # start of the string 
\( # match '('
? # match between zero and one time
( # get the enclosed match
[0-9] # match a digit
{3} # matching for 3 x digit
) # end enclosed match
\) # match ')'
? # match between zero and one time
[-. ] # match from '-' and on..
? # match between zero and one time
( # get enclosed match
[0-9] # match a digit
{3} # matching for 3 x digit
) # end enclosed match
[-. ] # match from '-' and on..
? # match between zero and one time
( # get enclosed match
[0-9] # match a digit
{4} # match for 4 x digit
) # end enclosed match
$ # end of string


 
 


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

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