In a string I want to find all matches of a regex in a string, save the matches and replace the matches. Is there a *slick* way to do that?

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)
# $text -> 'a xxx a xxx the xxx some xxx for a xxx'
# @matches -> qw(cat hat bat fat rat)

```

I've tried: `@matches = ($text =~ s{($re)}{xxx}g)` but it gives me a count.

Do I have to add some executable code onto the end of pattern `$re`?

Update: Here is a method which uses the code execution extended pattern `(?{ ... })`:

```
use re 'eval';  # perl complained otherwise
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;

```