in reply to Re^2: variable interpolation in regexps
in thread variable interpolation in regexps
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;
|
|---|