Alex Rivera | Logout

How do you match accented and tilde characters in a perl regular expression (regexp)?

Asked 2011-03-01T16:15:54.217
12

A user enters a set of names with accents and tildes:

Renato Núñez, David DeJesús, and Edwin Encarnación 

My database has anglicized names for these people

@names = ('Renato Nunez','David DeJesus','Edwin Encarnacion');

I wish to do a regexp match on these names.

$string = "Renato Núñez, David DeJesús, and Edwin Encarnación";
foreach my $name (@names) {
    print "found:$name\n" if ($name =~ /$string/);
}

As currently presented I get no matches.

I tried this, but it didn't work.

$string = "Renato Núñez, David DeJesús, and Edwin Encarnación";
foreach my $name (@names) {
    $name =~ s|a|[áa]|;
    $name =~ s|e|[ée]|;
    $name =~ s|i|[íi]|;
    $name =~ s|o|[óo]|;
    $name =~ s|u|[úu]|;
    $name =~ s|n|[ñn]|;
    # Originally: print "found:$name\n" if ($name =~ /$string/);
    # Corrected to:
    print "found:$name\n" if ($string =~ /$name/);
}

EDIT: sorry I had $name and $string reversed in the last line.

Any suggestions?

Edit
Report

1 Answer

1

This might be more in line with what your trying to do.

use strict;
use warnings;

my @AngloNames = ('Renato Nunez','David DeJesus','Edwin Encarnacion');
my @AngEthRx;

for my $val (@AngloNames) {
   $_ = $val;
   s/a/[áa]/g;
   s/e/[ée]/g;
   s/i/[íi]/g;
   s/o/[óo]/g;
   s/u/[úu]/g;
   s/n/[ñn]/g;
   push @AngEthRx, $_;
}

# User input query string ...
my $AngEthQuery = "Renato Núñez, David DeJesús, and Edwin Encarnación";

for my $i (0 .. $#AngEthRx) {
   if ( $AngEthQuery =~ /($AngEthRx[$i])/ ) {
      print "found: $AngloNames[$i] ~ $1\n";
   }
}

out

found: Renato Nunez ~ Renato Núñez
found: David DeJesus ~ David DeJesús
found: Edwin Encarnacion ~ Edwin Encarnación

answered 2011-03-01T18:10:04.887

Your Answer