开发者

perl: best way to match, save and replace a regex globally

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 开发者_如何学Pythonhat 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;


If by "slick" you mean "employs uncommonly-used language features" or "will make other programmers scratch their heads," then maybe this is the solution for you:

my ($temp, @matches);

push @matches, \substr($text, $-[0], $+[0] - $-[0]) while $text =~ /\wat/g;

$temp = $$_, $$_ = 'xxx', $_ = $temp for reverse @matches;


This is similar to the approach in your Update, but a bit easier to read:

$text =~ s/($re)/push @x, $1; 'xxx'/ge;

Or this way (probably slower):

push @x, $1 while $text =~ s/($re)/xxx/;

But, really, is there anything wrong with unslick?

my @x = $text =~ /($re)/g;
$text =~ s/($re)/xxx/g;


my @x = map { $str =~ s/$_/xxx/; $_ } $str =~ /($re)/g;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜