0

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

3
  • You need pattern1 = r'\\b(?:(?!\\b)[^:])*:\\b' Commented Mar 11, 2024 at 13:26
  • it shoul work with a input_string.split("tt") with this example, can you give a more detailled explination on your requirements. Commented Mar 11, 2024 at 13:59
  • Use .*(tt.*tt) Commented Mar 11, 2024 at 14:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.