Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by misterperl (Sexton)
on Jul 28, 2011 at 13:38 UTC ( #917269=perlquestion: print w/ replies, xml ) Need Help??
misterperl has asked for the wisdom of the Perl Monks concerning the following question:

I guess this might be one of those look-ahead assertions or something, but frequently I'm seeking something like

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

I also have more complex conditions such as:

scalar starts with "cat" but not ones followed anywhere with "dog" except if its "dogs", or "dog" followed or preceeded by "collie".

I usually end up writing 2 or more regexs as cascading filters for these, and loop over the array- both undesirable since I'd prefer to use map(), and I think this could be one regex.

Wisdom please?

Comment on Im not sure what this regex is called or how to write it
Re: Im not sure what this regex is called or how to write it
by Anonymous Monk on Jul 28, 2011 at 13:50 UTC
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
    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
      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.
Re: Im not sure what this regex is called or how to write it
by jpl (Monk) on Jul 28, 2011 at 17:25 UTC
    There's nothing map can do that cannot be accomplished by a loop. Unless you are after one-liner bragging rights, your cascading filters are likely to be a good deal easier to understand (and extend, and get right) than a single complicated regular expression. My advice would be to use the cascading filters unless there is a really compelling need not to.
      >> There's nothing map can do that cannot be accomplished by a loop. Unless you are after one-liner bragging rights.... Sadly, my first programming language I learned, c 1977, was APL, and henceforth I was *conditioned* to believe anything over 1 line was excess.. My Bad!
        ... first programming language I learned ... *conditioned* to believe ...

        Another example of the "Rubber Duckie Syndrome."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-10-23 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls