Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Regex can't find match

by sherab (Scribe)
on Apr 07, 2014 at 18:56 UTC ( [id://1081427]=perlquestion: print w/replies, xml ) Need Help??

sherab has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks! I am generally ok with regexes but this one has me stumped. I am doing a massive search and replace operation across a few hundred pages and I seem to have a problem getting my match found. I have enough code below to recreate the problem.... (the href reference is an exact cut and paste).

#!/usr/bin/perl $text = 'YAY! <a href="/cgi-bin/ruling?candy=265-23&path=/rules/other/ +265-23&file_num=265-23&action=Show_Form" class="subCat9">Click to sub +mit</a> YAY!'; $find='<a href="/cgi-bin/ruling?candy=265-23&path=/rules/other/265-23& +file_num=265-23&action=Show_Form" class="subCat9">Click to submit</a> +'; $replace="This is a replacement"; # Would like to see # YAY! This is a replacement YAY! $text=~s/$find/$replace/g; print $text; # I get # YAY! <a href="/cgi-bin/ruling?candy=265-23&path=/rules/other/265-23& +file_num=265-23&action=Show_Form" class="subCat9">Click to submit</a> + YAY!


I am guessing it's some sort of interpolation issue maybe? Does anyone have a clue ?
Thank you so much for your advice. Sherab

Replies are listed 'Best First'.
Re: Regex can't find match
by toolic (Bishop) on Apr 07, 2014 at 18:59 UTC
    quotemeta
    $find=quotemeta '<a href="/cgi-bin/ruling?candy=265-23&path=/rules/oth +er/265-23&file_num=265-23&action=Show_Form" class="subCat9">Click to +submit</a>';
      OMG toolic, you have saved me so much trouble. Thank you very much. Now to get familiar with quotemeta.

        Specifically, the  ? quantifier regex metacharacter. See Quantifiers in perlre. Instead of quotemeta, you could also use
            $text=~s/\Q$find\E/$replace/g;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1081427]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found