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

Replies are listed 'Best First'.
Re^2: Im not sure what this regex is called or how to write it
by misterperl (Pilgrim) on Apr 20, 2012 at 14:34 UTC
    Thank-You this was very helpful. Far moreso than *see the documentation* which of course could be an automated reply to ANY and ALL inquiries.