In the string, I want to find all the regular expression matches in the string, save the matches, and replace the matches. Is there any way to do this?
Example:
my $re = qr{\wat};
my $text = "a cat a hat the bat some fat for a rat";
... (substitute $re -> 'xxx' saving matches in @matches)
I tried: @matches = ($text =~ s{($re)}{xxx}g)but he gives me the bill.
Should I add executable code at the end of the template $re?
Update: Here is a method that uses an extended code execution pattern (?{ ... }):
use re 'eval';
my $re = qr{\wat};
my $text = "a cat a hat the bat some fat for a rat";
my @x;
$text =~ s{ ($re)(?{ push(@x, $1)}) }{xxx}gx;
say "text = $text";
say Dumper(\@x); use Data::Dumper;
source
share