For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2005 > regular expression match problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regular expression match problem
Pine Yan

2005-10-25, 6:56 pm


This is a segment of code to do string search:

my $email = "\pineyan";
my $name = "\\pine";

if($email =~ /($name)/) {
print "Found my name: $name!\n";
}

and I got the following error when running:

Can't find unicode character property definition via main->i or
i.pl at unicode/Is/i.pl line 0

How can I get this match work?

Sincerely
Pine


Paul Lalli

2005-10-25, 6:56 pm

Pine Yan wrote:
> This is a segment of code to do string search:
>
> my $email = "\pineyan";


What are you expecting that string to contain? Do you think the first
two characters are a backslash and the letter 'p'? They aren't.
Double quoted strings do backslash interpolation. Either escape the \,
or use single quotes instead:
my $email = "\\pineyan";
or
my $email = '\pineyan';

> my $name = "\\pine";


This one, on the other hand, does contain one single backslash,
followed by the letter p.

>
> if($email =~ /($name)/) {


Now you've passed a string containing backslash-p to the regexp engine.
"\p" means something special in regular expressions. You want to
escape any meta-characters that happen to be in $name:

if ($email =~ /\Q$name\E/) {
#parentheses removed, because you didn't use any captured value.


> print "Found my name: $name!\n";
> }
>
> and I got the following error when running:
>
> Can't find unicode character property definition via main->i or
> i.pl at unicode/Is/i.pl line 0
>
> How can I get this match work?


Read more on regular expressions in any or all of the following:
perldoc perlre
perldoc perlretut
perldoc perlrequick
perldoc perlreref

Paul Lalli

Jeff 'japhy' Pinyan

2005-10-25, 6:56 pm

On Oct 25, Pine Yan said:

> This is a segment of code to do string search:
>
> my $email = "\pineyan";
> my $name = "\\pine";
>
> if($email =~ /($name)/) {
> print "Found my name: $name!\n";
> }
>
> and I got the following error when running:
>
> Can't find unicode character property definition via main->i or
> i.pl at unicode/Is/i.pl line 0


Perl sees your regex as /(\pine)/, and \pi is a shortcut for \p{i} which
means "Unicode property 'i'". To get around this, do:

if ($email =~ /(\Q$name\E)/) { ... }

The \Q...\E auto-quotes any regex-related characters, so that they're
matched literally.

P.S. My last name is Pinyan (awfully close to your full name). It's
pronounced the same way, I'm told, as "pinyin", the process by which
Chinese characters are transliterated into English "words".

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com