Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regex return matches from substitution

I am trying to simultaneously remove and store (into an array) all matches of some regex in a string. To return matches from a string into an array, you could use

my @matches = $string=~/$pattern/g;

I would like to use a similar pattern for a substitution regex. Of course, one option is:

my @matches = $string=~/$pattern/g;
$string =~ s/$pattern//g;

But is there really no way to do this without running the regex engine over the full string twice? Something like

my @matches = $string=~s/$pattern//g

Except that this will only return the number of subs, regardless of list context. I would also take, as a consolation prize, a method to use qr// where I could simply modify the quoted regex to to a sub regex, but I don't know if that's possible either (and that wouldn't preclude searching the same string twice).

like image 642
Andrew Schwartz Avatar asked Mar 21 '23 06:03

Andrew Schwartz


1 Answers

Perhaps the following will be helpful:

use warnings;
use strict;

my $string  = 'I thistle thing am thinking this Thistle a changed thirsty string.';
my $pattern = '\b[Tt]hi\S+\b';

my @matches;
$string =~ s/($pattern)/push @matches, $1; ''/ge;

print "New string: $string; Removed: @matches\n";

Output:

New string: I   am    a changed  string.; Removed: thistle thing thinking this Thistle thirsty
like image 172
Kenosis Avatar answered Mar 28 '23 06:03

Kenosis