Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Problem with negative lookahead at end of string

by olivierp (Hermit)
on Jun 21, 2004 at 15:00 UTC ( #368464=perlquestion: print w/replies, xml ) Need Help??
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:


If I include the whatever part, as here


, sample 2 still matches, whereas this:


excludes sample 2 but is horrible...

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


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

Replies are listed 'Best First'.
•Re: Problem with negative lookahead at end of string
by merlyn (Sage) on Jun 21, 2004 at 15: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...
Re: Problem with negative lookahead at end of string
by tye (Sage) on Jun 21, 2004 at 15:12 UTC

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


    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 15: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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://368464]
Approved by pelagic
Front-paged by grinder
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-06-22 11:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.