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;