Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Im not sure what this regex is called or how to write it

by moritz (Cardinal)
on Jul 28, 2011 at 13:54 UTC ( #917273=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Im not sure what this regex is called or how to write it
Select or Download Code
Re^2: Im not sure what this regex is called or how to write it
by misterperl (Sexton) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://917273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-11-29 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls