0

as we know the python '?' quantifier is consider a lazy quantifier and then should gives us the shortest match, but in my example i am getting the second match(second_occurrence), instead of the first match(first_occurrence)

content = "this is how we want that first_occurrence over there but that second_occurrence it is 
always wrong when "
match = re.search(r"^this .* that (?P<occurrence>.*?) ", content)
print(match.groupdict())
1
  • >>> match = re.search(r"^(this .* that )(?P<occurrence>.*?) ", content) >>> print(match.group(1)) this is how we want that first_occurrence over there but that Commented Dec 4, 2019 at 18:58

1 Answer 1

1

In your expression "^this .* that (?P<occurrence>.*?) " the first .* is greedy, so that it will match all the way to the last that.

Change your example to:

import re

content = "this is how we want that first_occurrence over there but that second_occurrence it is always wrong when "
match = re.search(r"^this .*? that (?P<occurrence>.*?) ", content)
print(match.groupdict())

This prints:

{'occurrence': 'first_occurrence'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.