in reply to pattern matching once
Follwing is a "pure regex" approach. It may of interest/useful if:
Win8 Strawberry 5.8.9.5 (32) Fri 08/11/2023 14:59:13 C:\@Work\Perl\monks >perl use strict; use warnings; use Test::More; use Test::NoWarnings; my @Tests = ( 'ALL these should match', # match # string success [ '<FILENAME>.htm' => 1, ], [ '<FILENAME>dp198076_424b2-us2342673.htm' => 1, ], [ 'fizz <FILENAME>dp198076_424b2-us2342673.htm fuzz' => 1, ], 'NONE of these should match', [ '' => '', ], [ ' ' => '', ], [ 'xxx' => '', ], [ "\n\n\n" => '', ], [ '<FILENAME>dp198076_424b2-us2342673.htm <FILENAME>dp198076_exfilingfees.htm' => '', ], [ '<FILENAME>.htmfred' => '', ], # see pm#11153807 [ 'fizz <FILENAME>dp198076_424b2-us2342673.htm foo bar <FILENAME>dp198076_exfilingfees.htm fuzz' => '', ], [ 'fizz <FILENAME>dp198076_424b2-us2342673.htm foo <FILENAME>xxx.htm bar <FILENAME>dp198076_exfilingfees.htm fuzz' => '', ], [ '<FILENAME>xxx.htm<FILENAME>yyy.htm' => '', ], [ '<FILENAME>.htm<FILENAME>.htm' => '', ], ); # end @Tests my @additional = qw(Test::NoWarnings); # each of these adds 1 test plan 'tests' => (scalar grep { ref eq 'ARRAY' } @Tests) + @additional ; my $rx_once = qr{ <FILENAME> (?: (?! [.]htm) .)* [.]htm \b }xms; VECTOR: for my $ar_vector (@Tests) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; my $got = $string =~ m{ \A (?: (?! $rx_once) .)* # no match before single match $rx_once # match just once (?: (?! $rx_once) .)* # no match after single match \Z }xms; is $got, $expected; } # end for VECTOR ^Z 1..14 # ALL these should match ok 1 ok 2 ok 3 # NONE of these should match ok 4 ok 5 ok 6 ok 7 ok 8 ok 9 ok 10 ok 11 ok 12 ok 13 ok 14 - no warnings
Update: Note that as the code stands, the test vector
[ '<FILENAME>.htm<FILENAME>.htmfred' => 1, ]
will match/pass, i.e., will be accepted as having no duplication. Should this be the case?
Give a man a fish: <%-{-{-{-<
|
---|