#/usr/bin/perl -w
use strict;
my $text= "start \\chapter{hello}\n end\n";
my $inpattern = qr/\\chapter\{(.*)\}/;
my $outpattern = q{" -- FOUND $1 -- "};
$text =~ s/$inpattern/$outpattern/gee;
print "$text\n";
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
Does the following come close to what you are looking for? I've modified your snippet so that a pattern is actually read from a file. Putting the s/// operation inside a string eval is an effective way to do what you want (my version does have "FOUND hello" in its output ). Note the backslash before "$text", so that this variable does not get interpolated into the string before it is evaluated as code:
#!/usr/bin/perl
use strict;
use warnings;
my $text = "start \\chapter{hello}\n end\n";
my $patternreadfromfile = <DATA>;
chomp $patternreadfromfile;
my $inpattern = qr/\\chapter\{(.*)\}/;
eval "\$text =~ s/$inpattern/$patternreadfromfile/g";
print "$text\n";
__DATA__
-- FOUND $1 --
(update: sorry, this node should have been a reply to your other node in this thread, at Re^2: simple regular expression) | [reply] [d/l] |
Fletch is quite right of course (I think; haven't tested it personally), but as a matter of curiosity, why do you not take the simpler approach of:
$text =~ s/$inpattern/ -- FOUND $1 -- /g;
No doubt this is just a part of a much larger script or perhaps an exercise, but, as I say, I was just curious. | [reply] [d/l] |
indeed, it is part of a larger exercise. and thank you.
| [reply] |
In my quest to simplify, I simplified too much. Let me try again:
#!/usr/bin/perl -w
use strict;
my $text= "start \\chapter{hello}\n end\n";
my $patternreadfromfile= "-- FOUND \$1 -- ";
my $inpattern = qr/\\chapter\{(.*)\}/;
my $outpattern = q{$patternreadfromfile};
$text =~ s/$inpattern/$outpattern/gee;
print "$text\n";
now q{} escapes too much...
| [reply] [d/l] |
With nested regex evaluations like m//ee s///ee, you have to
be very clear about what expression you are operating on at
each stage of /e-valuation. It's best to work backwards.
In your latest example, and working backwards from the regex
replacement field, the expression $outpattern,
which was defined with single- (i.e., non-interpolating) quotes,
evaluates to the string '$patternreadfromfile', literally. (The single-quotes are
not part of the string.)
Evaluating again (the second e in the /gee),
the expression $patternreadfromfile evaluates to the
string '-- FOUND $1 --'. This string is what is
substituted into the original string.
It's as if you just need one more level of double-quotish
interpolation to get what you want!
If the first evaluation yielded the string
'qq{-- FOUND $1 -- }', you could evaluate that
a second time as an expression to get what you want.
So $outpattern could be defined
as qq{ qq{$patternreadfromfile} } and
you're home and dry!
I hope this rather involved and, I hope, not too incoherent explanation will be helpful.
(BTW -- I assume the multiple levels of string definition and interpolation are needed in the final application. In your second code example as it stands, they are not; $patternreadfromfile could be interpolated more directly.)
use warnings;
use strict;
my $text= "start \\chapter{hello}\n end\n";
my $patternreadfromfile= "-- FOUND \$1 -- ";
my $inpattern = qr/\\chapter\{(.*)\}/;
# my $outpattern = q{$patternreadfromfile};
# # $outpattern eq '$patternreadfromfile'
# # outputs: 'start -- FOUND $1 --\n end'
my $outpattern = qq{ qq{$patternreadfromfile} };
# $outpattern eq 'qq{-- FOUND $1 -- }'
# eval($outpattern) eq '-- FOUND capture_group_1_contents -- '
# outputs: what you want (i think)
$text =~ s/$inpattern/$outpattern/gee;
print "$text\n";
Update:
... $patternreadfromfile could be interpolated more directly. What I had in mind was that the pattern could be interpolated in just one evaluation step. On further consideration, I don't think this can be done. (But see graff's note below on an approach not involving s///e substitution replacement evaluation.)
-
2017 Feb 18: Just noticed that I was trying to apply an /e modifier to a m// match in the first paragraph above. Don't know where that came from. Fixed.
| [reply] [d/l] [select] |