Tiago Stein
Tiago Stein

Reputation: 11

Python regex non greedy not matching substring when the pattern has string surrounding the variable part

I hate to post such specific questions as why a regex is not working, but I have been searching for someone with the same problem to no avail. It seems I am miss understanding something about greedy regex:

I am using python for this:

input_string = "tt_firstbutnotlast_tt_second_tt"
pattern1 = "(tt.+tt)?"

I want to match the "second" part, but even with the "?" It always matches the whole string. I thought "?" would make it match the smallest string that can match it. Can someone explain to me what I am miss understanding about the greedy part?

note: I simplified the string example to make it more clear

Upvotes: 0

Views: 48

Answers (0)

Related Questions