CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Dimka
New User

Dec 25, 2011, 1:56 PM

Post #1 of 4 (4476 views)
reg expressions - strange berhavior Can't Post

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

Post #2 of 4 (4459 views)
Re: [Dimka] reg expressions - strange berhavior [In reply to] Can't Post

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

Post #3 of 4 (4439 views)
Re: [BillKSmith] reg expressions - strange berhavior [In reply to] Can't Post

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

Post #4 of 4 (4415 views)
Re: [Dimka] reg expressions - strange berhavior [In reply to] Can't Post

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)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives