|
Description:
This regular expression matches valid postal codes as defined by the Royal Mail
(the UK's postal company).
Usage: Text Source
use strict;
my @patterns = ('AN NAA', 'ANN NAA', 'AAN NAA', 'AANN NAA',
'ANA NAA', 'AANA NAA', 'AAA NAA');
foreach (@patterns) {
s/A/[A-Z]/g;
s/N/\\d/g;
s/ /\\s?/g;
}
my $re = join '|', @patterns;
while (<>) {
print /^(?:$re)$/o ? "valid\n" : "invalid\n";
}
The license for this recipe is available here.
Discussion:
This regular expression comes to us from Craig Berry from a posting
in the comp.lang.perl.misc newsgroup (article 30293132).
The regular expression is built by grokking the @patterns array, replacing A's
with [A-Z], N's with \d, and spaces with \s? and combining the elements of
the array with branch operators (i.e., the pipe character, '|').
|