[Melbourne-pm] Regular expression fun

David Dick ddick at iinet.net.au
Thu Sep 4 03:06:55 PDT 2014


On 09/04/2014 06:32 PM, Jacinta Richardson wrote:
> Does anyone have any suggestions for how that might be done?

Or this?

my $page = "asdfasd fasdf asdf asdf Smith John";
print "Page Contents:$page\n";

my %clients = (
     031234 => "John Smith",
      234345 => "Jane Brown",
      345345 => "Jameel Bayan"
);

my %matches;
foreach my $client_id (keys %clients) {
     my $client = $clients{$client_id};
     $matches{$client} = $client_id;

     my ($first, $last) = split /\s+/, $client;
     my $first_initial_only = substr $first, 0, 1;
     $matches{$first_initial_only} = $client_id;

     my $match_first_initial_last = qr{$first_initial_only\s+$last};
     my $match_surname_first   = qr{$last\W+$first};
     $matches{$match_surname_first} = $client_id;
}

my $regex = join q[|], map { '(' . $_  . ')' } keys %matches;
my ($count, $index) = (-1, 0);
my $matched = grep { defined $_ ? $index = $count : $count += 1; } $page 
=~ /$regex/;
print "Found " . $clients{$matches{substr((split /[|]/, $regex)[$index], 
1, -1)}} . "\n";



More information about the Melbourne-pm mailing list