In this case, you could also use s///ee (double eval).

my $line = 'The quick <CM>HTMLINSERT:<img src="grab me!"></CM> brown f +ox'; my $key = '<CM>HTMLINSERT:<img src=([^>]+)></CM>'; my %fsr_hash; $fsr_hash{$key} = '"<figure><graphic url=$1/></figure></p>"'; $line =~ s/$key/$fsr_hash{$key}/gee; print "$line\n";

___

$ ./701504.pl The quick <figure><graphic url="grab me!"/></figure></p> brown fox

Unless I've overlooked something (which I'm sure someone would point out :), the usual worries about possibly executing arbitrary Perl code do not apply here, because whatever user input is in $1, despite the double eval it won't be evaluated — i.e. something like ... src=@{[...some evil code...]} ... in the input, will just produce

The quick <figure><graphic url=@{[...some evil code...]}/></figure></p +> brown fox

(Of course, allowing arbitrary HTML code to be included could also be problematic... but that's another issue...)


In reply to Re: backreferencing fails in a search and replace with a hash by almut
in thread backreferencing fails in a search and replace with a hash by tonyz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.