I assume that a correct solution handles cases where both foobar and a non-foo-prefixed bar appear in the same input string. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

"The aim of the code is to match 'bar' that is NOT directly preceeded by 'foo'."

A strict read of this says that the string "okbar and foobar" should match, based on the first 'bar'. And the loop unrolling hint does not suggest otherwise. And if all you want to do is reject any line containing "foobar", then

next if /foobar/;

is sufficient. That's hardly interesting.

(Didn't expect this to become a side lesson in requirements analysis, did you? :-)

This might be more along the lines of what you're looking for: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

m{ ^ # start of string [^f]* # 0 or more non-f's (?: # followed by f+ # 1 or more f's (?: # followed by o? [^o] # 0 or 1 o followed by a non-o | # or ooo+ # 3 or more o's ) [^f]* # followed by 0 or more non-f's )* # the non-foo part 0 or more times bar # followed by 'bar' }x </pre><HR></BLOCKQUOTE>