c:\@Work\Perl>perl -wMstrict -le
"use Test::More 'no_plan';
use Test::NoWarnings;
;;
my $metadata = qr{ < [^>]* > }xms;
;;
for my $ar_vector (
[ 'somewordaa<metadata><moremetadata>r another wo<metadata>rd',
'somewordaa<metadata><moremetadata>r another wo<metadata>rd',
],
[ 'aword<metadata>aa<metadata> another wo<metadata>rd',
'aword<metadata>a<metadata> another wo<metadata>rd',
],
[ 'wor<metadata>dsandmor<metadata>ewordsaa<metadata>',
'wor<metadata>dsandmor<metadata>ewordsa<metadata>',
],
) {
my ($s, $expected) = @$ar_vector;
$s =~ s{ aa (?! $metadata* r) }{a}xmsg;
is $s, $expected, qq{'$expected'};
}
"
ok 1 - 'somewordaa<metadata><moremetadata>r another wo<metadata>rd'
ok 2 - 'aword<metadata>a<metadata> another wo<metadata>rd'
ok 3 - 'wor<metadata>dsandmor<metadata>ewordsa<metadata>'
ok 4 - no warnings
1..4
Note that making the character class match 'atomic' or possessive may speed things up a bit for large data (for some definition of 'large') by suppressing backtracking on subsequent match failure:
my $metadata = qr{ < (?> [^>]*) > }xms;
and for Perl version 5.10 and above:
my $metadata = qr{ < [^>]*+ > }xms;
(both variations tested). Your data will determine if possessiveness actually makes a difference. The regex compiler may be smart enough to make this optimization on its own (I haven't checked), but giving the compiler a hint never hurts.
And of course, you will want to add many more test cases!
|