Negative Lookbehind and Wildcards

Discussion in 'Perl' started by Thomas F. O'Connell, Feb 27, 2004.

  1. I've been looking through the negative lookbehind posts and haven't
    yet found a definitive answer to the question I'm about to ask:

    Does negative lookbehind have lower precedence than even a non-greedy
    wildcard (*) in the Perl regular expression engine?

    The reason I ask is the following scenario:

    I am try to match a pattern B as long as it is not preceded by a
    pattern A in a given string, regardless of what occurs between B and
    A.

    To me, this seems a natural use of negative lookbehind. But when I try
    the following expression:

    /(?!<A).*B/

    it does not work as I expect it to. I've tried both greedy and
    non-greedy wildcards, and the only thing I can think is that the
    wildcard takes precedence in matching up to B because of the rules of
    matching in the engine. Is that correct?

    Is there a way to solve this problem?

    I've got Perl 5.8.0 for Linux.

    Thanks!

    -tfo
    Thomas F. O'Connell, Feb 27, 2004
    #1
    1. Advertising

  2. Thomas F. O'Connell wrote:
    > I am try to match a pattern B as long as it is not preceded by a
    > pattern A in a given string, regardless of what occurs between B
    > and A.
    >
    > To me, this seems a natural use of negative lookbehind. But when I
    > try the following expression:
    >
    > /(?!<A).*B/


    Suppose you mean

    /(?<!A).*B/

    But that does not help, since .* matches anything, including pattern
    A, from the beginning of the string.

    > it does not work as I expect it to. I've tried both greedy and
    > non-greedy wildcards, and the only thing I can think is that the
    > wildcard takes precedence in matching up to B because of the rules
    > of matching in the engine. Is that correct?


    I don't think it is about precedence. The problem is that a dot
    matches any character.

    > Is there a way to solve this problem?


    One way might be to replace the dot with a character class that does
    not include one or more characters that are included in pattern A.

    If you know that pattern B can only occur once, I suppose it's easiest
    to simply use two regexes:

    /B/ and !/A.*B/

    HTH

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mail
    Replies:
    1
    Views:
    531
    Will Stranathan
    Mar 2, 2004
  2. Replies:
    2
    Views:
    552
  3. Isaac Councill

    negative lookbehind and \.

    Isaac Councill, Jan 10, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    129
    Malcolm Dew-Jones
    Jan 10, 2004
  4. Isaac Councill

    negative lookbehind and \. [ADDENDUM]

    Isaac Councill, Jan 10, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    97
    Isaac Councill
    Jan 10, 2004
  5. Peng Yu

    Negative Lookbehind and Wildcards

    Peng Yu, Jun 13, 2010, in forum: Perl Misc
    Replies:
    3
    Views:
    240
    C.DeRykus
    Jun 14, 2010
Loading...

Share This Page