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


In reply to Re: RegEx - match !foo followed by foo by johngg
in thread RegEx - match !foo followed by foo by Melly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.