user3552664 user3552664 - 3 months ago 20
Python Question

How to max regex non greedy working backwards

I always assumed regex worked like this, but I guess I never hit a case like this until now and I'm not sure the best way to tackle it.

String to consider:

apple
apple
apple
cat


I want to use something like apple.*?cat, however, this matches the first apple to the cat when I really want the last apple and cat.

Please keep in mind this is just an example, I'm looking for a generalized way to do this (ie telling me to just match one newline between apple and cat won't work in my real case)

Answer

You can use this negative lookahead based on tempered greedy token regex in python:

reg = re.compile(r'apple(?:(?!apple).)*cat', re.DOTALL)

RegEx Demo

(?:(?!apple).)* will match 0 or more any character that don't have apple at next position thus making sure we don't have apple in our match. Note that negative lookahead will be asserted for each character in the match.