Search code examples
python-3.xregexregex-lookarounds

Lookarounds with lazy quantifiers


I'm trying to use a lookbehind followed by a lazy "match everything pattern" (.+?) in a regex, but I'm wondering if I can use lookarounds this way.

For example, I have the following regex : (?<!learn).+?write a regex

  • "I'm learning how to write a regex" (should not match)
  • "I know how to write a regex" (should match)
  • "I know how to read an write a regex" (should match)
  • "I want to know how to write a regex" (should match)
  • "I want to learn how to write a regex" (should not match)

If you use the regex above, you get everything matched.


Solution

  • Your pattern matches all lines, because (?<!learn).+?write a regex will run the lookbehind at the first position, asserting what is directly to the left of the current position is not learn

    That assertions is true and this part will immediately match until the first occurrence of write a regex

    What you can do, is make use of the PyPi regex module which supports an infinite quantifier in the lookbehind:

    (?<!\blearn.+?)\bwrite a regex\b
    

    Regex demo | Python demo

    import regex
    
    pattern = r"(?<!\blearn.+?)\bwrite a regex\b"
    
    strings = [
        "I'm learning how to write a regex",
        "I know how to write a regex",
        "I know how to read an write a regex",
        "I want to know how to write a regex",
        "I want to learn how to write a regex"
    ]
    
    for s in strings:
        if regex.search(pattern, s):
            print(s)
    

    Output

    I know how to write a regex
    I know how to read an write a regex
    I want to know how to write a regex