use strict; use warnings; use Test::More qw(no_plan); my $str = 'blfoo and barthisfoothatqux and barsofooquxhim andfoosom foo'; my $expected = 'bl123 and barthisfoothatqux and barsofooquxhim and123som 123'; $str =~ s[(bar.+?qux)|(foo)][defined $2 ? '123' : $1]xge; is($str,$expected); #switch qux and bar $str = 'blfoo and quxthisfoothatbar and barsofooquxhim andfoosom foo'; $expected = 'bl123 and barthisfoothatqux and barsofooquxhim and123som 123'; $str =~ s[(bar.+?qux)|(foo)][defined $2 ? '123' : $1]xge; is($str,$expected);