Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
The stupid question is the question not asked
 
PerlMonks  

Problem with negative lookahead at end of string

by olivierp (Hermit)
on Jun 21, 2004 at 11:00 UTC ( [id://368464]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

olivierp has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks ! I'm having trouble building a regex that would match as follows:
string starts with chars, contains this, but does not contain that and does not end with whatever A couple of samples:
  • chars at the beginning, this is right and whatever is not at the end
  • chars at the beginning, this is ok and the end is whatever
  • chars at the beginning, this is ok and that is included
  • chars at the beginning, that here this is not ok
This regex will match 1 and 2, and correctly skip 3 and 4:

^chars(?!.*that).*this(?!.*that).*

If I include the whatever part, as here

^chars(?!.*that).*this(?!.*that).*(?!.*whatever)$

, sample 2 still matches, whereas this:

^chars(?!.*that).*this(?!.*that).*[^w][^h][^a][^t][^e][^v][^e][^r]$

excludes sample 2 but is horrible...

Am I missing something with the negative lookahead, or is it not possible to do such a thing ?

--
Olivier

Edited by demerphq: Changed </br> to <br />

Replies are listed 'Best First'.
Re: Problem with negative lookahead at end of string
by tye (Sage) on Jun 21, 2004 at 11:12 UTC
    /^chars(?!.*that).*this.*(?<!whatever)$/

    If "whatever" is actually a pattern that can match more than a single length of string, then instead use:

    /^chars(?!.*that).*this(?!.*whatever$)/

    And note that the (?!.*that) part doesn't need to be repeated.

    - tye        

Re: Problem with negative lookahead at end of string
by delirium (Chaplain) on Jun 21, 2004 at 11:25 UTC
    Despite a processing slowdown, I prefer the readability of splitting criteria like this up into multiple expressions:

    /^chars/ && /this/ && !/that/ && !/whatever$/

      Despite a processing slowdown...

      It depends on the patterns and the string, but it's often faster (and in this case it is faster) to split up the matches into multiple regexes.

•Re: Problem with negative lookahead at end of string
by merlyn (Sage) on Jun 21, 2004 at 11:45 UTC
    For simplicity sake, you can list a compound "and-ed" condition as a series of null assertions:
    / ^ # anchor so as not to try this at every spot (?= chars ) # stars with chars (?= .*? this ) # contains this (?! .*? that ) # does not contain that (?! .* whatever $ ) # does not end in whatever /xs

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

    update: edited to include additional whitespace for readability
      Nice. "I'd walk a mile for a (Programming Perl) Camel." Had to reach for it to get through that ?!.*? regex! I'm just an old C programmer, learning more than one way to do it...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://368464]
Approved by pelagic
Front-paged by grinder
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.