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

36 Views Asked by At

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

0

There are 0 best solutions below