<> wrote:
> I have a string. I have several patterns to match for. I'd like
> to determine which pattern finds a match in the string first & at what
> position.
>
> Anyone have an elegant way to do this?
--------------------------------
#!/usr/bin/perl
use warnings;
use strict;
my $string = 'foo TX bar 12345 baz 98765-1234';
my %patterns = (
street => qr/\d+\s+.*/,
zip => qr/\d{5}(-\d{4})?/,
state => qr/\b[A-Z][A-Z]\b/,
);
print "$string\n";
print "01234567890123456789\n";
foreach my $type ( keys %patterns ) {
print "$type at ", length($1), "\n" if $string =~ /(.*?)$patterns{$type}/;
}
--------------------------------
Making note of the lowest value of length($1) is left as
an exercise for the reader.
--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas