in reply to RegEx - match !foo followed by foo

I think a good approach is to break the problem down into sections and make smaller compiled regular expression building blocks then put them together for the final solution. Hopefully, I've understood the requirement but the following seems to do the trick.

#!/usr/local/bin/perl -w # use strict; # Make some building blocks. What we want at the start; what we don't +want # after "foobar:" using a negative look-ahead assertion; any five # characters; what we want at the end (which we will discard later). # our $rxFooBar = qr{foobar:}; our $rxNotGonkish = qr{(?!(?:g(?:o(?:n(?:k)*)*)*|@))}; our $rxFive = qr{.{1,5}}; our $rxGonk = qr{(?:gonk|@)}; # Put the building blocks together using "()" to capture everything ex +cept # the "gonk" or "@" at the end; # our $rxPutItAllTogether = qr{($rxFooBar$rxNotGonkish$rxFive)$rxGonk}; while(<DATA>) { chomp; print "$_\n"; print /$rxPutItAllTogether/ ? "$1\n\n" : "Failed\n\n"; } __END__ foobar:hellogonk foobar:gonk foobar:higonk foobar:helloworldgonk foobar:ggonk foobar:gogonk foobar:gongonk foobar:gonkgonk foobar:gonkygonk foobar:@gonk foobar:its@ foobar:toomany@
Which produces the following output.

foobar:hellogonk foobar:hello foobar:gonk Failed foobar:higonk foobar:hi foobar:helloworldgonk Failed foobar:ggonk Failed foobar:gogonk Failed foobar:gongonk Failed foobar:gonkgonk Failed foobar:gonkygonk Failed foobar:@gonk Failed foobar:its@ foobar:its foobar:toomany@ Failed
Cheers,

JohnGG