I now realize that I only saw half of the problem. The wanted idea seems to be double-interpolation of 'some$thing' into "someTHING" by replacing the string '$thing' within $regexp with the value of the variable $thing.

Your points are close but wrong - Perl allows stuff to appear in regular expressions after the $ metacharacter. Otherwise, Perl regular expressions act like double-quoted strings and hence interpolate only once:

#!perl -wl use strict; my $regexp = 'some$thing'; my $thing = 'THING'; my $target = 'this is some$thing strange.'; print $regexp; print $target; print '$target =~ /$regexp/ ', $target =~ /$regexp/; print '$target =~ /\Q$regexp\E/ ', $target =~ /\Q$regexp\E/; print '$target =~ /some\$thing/ ', $target =~ /some\$thing/; print '$target =~ /some$/ ', $target =~ /some$/; my $eol_in_the_middle = qr/some$(?:thing)/; print 'eol_in_the_middle ',$eol_in_the_middle; print '$target =~ /$eol_in_the_middle/ ',$target =~ /$eol_in_the_middl +e/;

Of course, it's kinda hard to make $eol_in_the_middle match, but you can do it by using the /m switch and changing the RE and target string a bit:

my $eol_in_the_middle2 = qr/some$(?:\s*thing)/; my $target2 = "this is some\nthing strange."; print '$target2 =~ /$eol_in_the_middle2/ ',$target2 =~ /$eol_in_the_mi +ddle2/sm;

In reply to Re^3: variable interpolation in regexps by Corion
in thread variable interpolation in regexps by Locutus

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.