http://www.perlmonks.org?node_id=917273


in reply to Im not sure what this regex is called or how to write it

scalar starts with "cat" but not ones followed anywhere with "dog".

Yes, a negative look-ahead:

use strict; use warnings; for (qw/cat dog catfoo catdog/) { print $_ =~ /^cat(?!dog)/ ? '[X]' : '[ ]', "$_\n"; } __END__ [X]cat [ ]dog [X]catfoo [ ]catdog
scalar starts with "cat" but not ones followed anywhere with "dog" except if its "dogs", or "dog" followed or preceeded by "collie".

If the "dog" is proceeded by "collie", then it's not "cat followed by dog" anymore, so we don't have to consider that case.

use strict; use warnings; for (qw/cat dog catfoo catdog catdogs catdogcollie/) { print $_ =~ /^cat(?!dog(?!s|collie))/ ? '[X]' : '[ ]', "$_\n"; } __END__ [X]cat [ ]dog [X]catfoo [ ]catdog [X]catdogs [X]catdogcollie