in reply to greedy subexpression between two nongreedy ones
As a general technique, here's a way to emulate [^set] where you are dealing with a potentially complex regex expression rather than a simple character class:
Where:c:\@Work\Perl\monks>perl -wMstrict -le "my $cd = qr{ cd }xms; my $not_cd = qr{ (?! $cd) . }xms; ;; for my $s (',abcdefg,pqrstuv', ',abefg,pqrstuv', @ARGV) { my $t = $s; print qq{'$t'}; $t =~ s{ , $not_cd* ($cd?) [^,]* , }{=$1=}xms; print qq{'$t' \n}; } " ',abcdefg,pqrstuv' '=cd=pqrstuv' ',abefg,pqrstuv' '==pqrstuv'
Update: Here's an example more in tune with your OPed assertion that the start and end delimiter patterns (and the optional included pattern) may be complex:
The .*? $end could be $excluded* $end instead. This would make the regex perhaps a bit more robust, but a bit slower.c:\@Work\Perl>perl -wMstrict -le "my $maybe = qr{ cd? }xms; ;; my $start = qr{ A | BC | DE?F }xms; my $end = qr{ U | VW | XY?Z }xms; my $excluded = qr{ (?! $end | $maybe) . }xms; ;; for my $s ('AxxcdxxVWABCDEFUVWXYZ', 'BCxxxUABCDEFUVWXYZ', @ARGV) { my $t = $s; print qq{'$t'}; $t =~ s{ $start $excluded* ($maybe?) .*? $end }{=$1=}xms; print qq{'$t' \n}; } " BCxxcxxXZABCDEFUVWXYZ 'AxxcdxxVWABCDEFUVWXYZ' '=cd=ABCDEFUVWXYZ' 'BCxxxUABCDEFUVWXYZ' '==ABCDEFUVWXYZ' 'BCxxcxxXZABCDEFUVWXYZ' '=c=ABCDEFUVWXYZ'
Give a man a fish: <%-(-(-(-<
|
|---|