Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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 ( [id://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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-28 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found