in reply to match with exception
One way (note that a piece of 'xyz', the 'yz', is captured – after all, 'yz' is not 'xyz'!):
Another way (note that the 'xyz' sequence is entirely excluded):>perl -wMstrict -le "my $s = 'abcxyzdef'; my $exclude = qr{ xyz }xms; my $all_not_xyz = qr{ (?! $exclude) . }xms; my @matches = $s =~ m{ ($all_not_xyz+) }xmsg; print qq{@matches}; " abc yzdef
These approaches generalize nicely to multiple exclusion sequences:>perl -wMstrict -le "my $s = 'abcxyzdef'; my $exclude = qr{ xyz }xms; my @matches = split m{ $exclude }xms, $s; print qq{@matches}; " abc def
>perl -wMstrict -le "my $s = 'abcFOOdefBARghi'; my $exclude = qr{ FOO | BAR }xms; my $all_but = qr{ (?! $exclude) . }xms; my @matches = $s =~ m{ ($all_but+) }xmsg; print qq{@matches}; @matches = split m{ $exclude }xms, $s; print qq{@matches}; " abc OOdef ARghi abc def ghi
|
|---|