Home: Perl Programming Help: Regular Expressions:
reg expressions - strange berhavior



Dimka
New User

Dec 25, 2011, 1:56 PM


Views: 8403
reg expressions - strange berhavior

Hi everybody,

I got some really annoying problem.
when i'm running this code:

my $line = ">gi|1222|gb|AAK60016.1|AF312325_1 maturase [Buchloe dactyloides]";
my $string = "ref|NM_001131814.1|";


if ($line =~ /$string/)
{
print "yes \n";
}
else
{
print "no \n";
}



It returns "yes" even so string does not exist within line.

Can anybody explain it to me?

Thanks


BillKSmith
Veteran

Dec 25, 2011, 7:42 PM


Views: 8386
Re: [Dimka] reg expressions - strange berhavior

The character "|" is special in a regular expression. It has to be escaped with a backslash to match itself. Unfortunately, the backslash is a special character in a string. It must be escaped with another backslash to put into the string. So the required string is:


Code
my $string = "ref\\|NM_001131814.1\\|";





The prefered syntax would be:


Code
my $string = qr/ref\|NM_001131814.1\|/;



Refer "Regex Quote-Like Operators" in perldoc perlop
Good Luck,
Bill


Dimka
New User

Dec 26, 2011, 8:19 AM


Views: 8366
Re: [BillKSmith] reg expressions - strange berhavior

thanks!


I have found out the best way to deal with this problem:

my $quoted = quotemeta $string;
($line ~= /$quoted/);


BillKSmith
Veteran

Dec 26, 2011, 11:23 AM


Views: 8342
Re: [Dimka] reg expressions - strange berhavior

Although your method "works", I would still recommend qr for regular expressions becuse you do not have to be concerned about a mixture of string and re syntax. Note: you can get the advantage of the quotemeta by using the equivalent re syntax \Q.


Code
 my $string = qr{\Qref|NM_001131814.1|};   
if ( $line =~ $string ) {

Good Luck,
Bill

(This post was edited by BillKSmith on Dec 26, 2011, 11:31 AM)