Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching -- driver's license number validation

Status
Not open for further replies.

phinsman

Programmer
Joined
Sep 23, 2003
Messages
6
Location
US
Hello all...

I'm trying to validate driver's license numbers for each state. I'm going to use a hash of patterns. Some states, such as Arkansas, my example below, have more than one possible method.

I'm getting the pattern to match properly on all three possibilities. However, I need to make sure that there isn't extra text at the beginning or ending of the user's input. Otherwise, the validation is useless. For example:

123-45-6789 is correct
123-45-6789extra_text is NOT correct.

I used the ^ and $ symbols. The ^ works fine; extra text at the beginning is disallowed, and it matches properly if there isn't any. The $ sign, however, isn't working for me. No matter what I try I can't get it to match. I have tried adding () around each possibility but that does not work either.

Code is below:

Code:
##########################################################
#!/usr/bin/perl

use strict;

#Use a hash for patterns
my %p = ();

#Valid patterns for Arkansas:
# 123456789     (SSN, no hyphens)
# 123-45-6789   (SSN with hyphens)
# 912345678     (9 then 8 digits)

$p{'AR'} = '^[\d]{3}[\d]{2}[\d]{3}$'.
           '|'.
           '^[\d]{3}\-[\d]{2}\-[\d]{3}$'.
           '|'.
           '^9[\d]{8}$';

#Sample driver license number
my $dl = '123456789';

#$match is just a variable that reports YES if a match if found
my $match = '';

#Check for a match
if($dl =~ /$p{'AR'}/) {
        $match = "YES";
}

#Output the driver license number, and whether it matched or not
my $line = sprintf("%-14s  %-4s",$dl,$match);
print $line,"\n";

#Exit the program
exit;
##########################################################

When I run the program as is I get this output:
123456789

No 'YES' for a match.

Thanks for any help.

Dale
 
All of your regex are matching on 8, 9 not digits


Change $dl to

my $dl = '12345678';


And it matches
--

^[\d]{3}[\d]{2}[\d]{3}$
# 3 + 2 + 3 == 8
^[\d]{3}\-[\d]{2}\-[\d]{3}$
3 + 2 + 3 == 8
^9[\d]{8}$
# 8 == 8
 
I tried to compact it a little:
Code:
#!/usr/bin/perl 

use strict; 

#Use a hash for patterns 
my %p = (
	 AR => '^(9\d{8}|\d{3}(-?)\d{2}\2\d{3})$'
	 );

foreach my $dl qw(
12345678
912345678
123-45-678
123-45678
12345-678
812345678
stuff123-45-678
123-45-678stuff
abc-de-fgh
) {
    my $valid = $dl =~ m/$p{'AR'}/;
    print "License number '$dl' is " . ($valid ? "valid" : "invalid") ."\n"
}

--------------------
"When all else has failed, read the manuals."

Denis
 
Wow...all that work and my only mistake was that I couldn't count!

Thanks a lot for your help guys. At least the problem is solved; I was truly frustrated with it but it's nice when it's something simple that doesn't really need fixing.

Thanks again!

Dale
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top