in reply to Re^2: Regex question - capturing next char
in thread Regex question - capturing next char
I would approach the problem this way:
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: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
And of course, you will want to add many more test cases!
|
|---|