RomainM
RomainM

Reputation: 199

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

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

Upvotes: 1

Views: 96

Answers (1)

The fourth bird
The fourth bird

Reputation: 163517

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

Upvotes: 1

Related Questions