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: Intermediate:
Regular expression foo

 



vijayneedshelp
New User

Jul 11, 2011, 11:42 PM

Post #1 of 3 (877 views)
Regular expression foo Can't Post

    my $pattern = "href=[\'\"](\/deals\/[0-9]+[^\?]+)\?msdc_id=[0-9]+[\'\"]";
print "yay\n" if $content =~ /href=[\'\"](\/deals\/[0-9]+[^\?]+)\?msdc_id=[0-9]+[\'\"]/;

print "yay\n" if $content =~ /$pattern/;


It only prints one "yay". Why does putting a pattern in a string not yield a match in the second case?


vijayneedshelp
New User

Jul 12, 2011, 12:13 AM

Post #2 of 3 (875 views)
Re: [vijayneedshelp] Regular expression foo [In reply to] Can't Post

Oh wow, I think I figured it out!

When you put a pattern in a string and use it as a regular expression, you need to double escape certain things.

Like if I want a literal match to the ? character I can't do:

$pattern = "\?";

I should do:

$pattern = "\\?";

That stumped me for ages!


macareus
New User

Jul 12, 2011, 1:30 AM

Post #3 of 3 (872 views)
Re: [vijayneedshelp] Regular expression foo [In reply to] Can't Post

 
You might want to look into the qr{} operator.
It quotes and interpolates the pattern; that is,
without rescanning.

Consult 'man perlop', and search for qr.

 
 


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

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