>perl -wMstrict -le "my %rx = ( FOO => qr{ (?i) foo (?! bar) }xms, BAR => qr{ (?i) bar (?= (?-i) foo) }xms, ); ;; my $s = 'xxx FooBar fOO BaRFoO bArfoo bar yyy'; for my $type (keys %rx) { print qq{parsing $type-ish stuff with $rx{$type}}; while ($s =~ m{ ($rx{$type}) }xmsg) { print qq{ found a '$1'}; } } " parsing BAR-ish stuff with (?msx-i: (?i) bar (?= (?-i) foo) ) found a 'bAr' parsing FOO-ish stuff with (?msx-i: (?i) foo (?! bar) ) found a 'fOO' found a 'FoO' found a 'foo'