import re
regex = re.compile(r"(.*)pl_\w\w(.*)(\?Id=\d+)?&?.*", flags=re.MULTILINE)
test_str = ("1. http://www.example.com/myplan/mp/public/pl_be?Id=543543×tamp=06280435435\n\n"
"2. http://www.example.com/course/df/public/pl_de?Id=454354×tamp=0628031746\n\n"
"3. http://www.example.com/book/rg/public/pl_fo?Id=4445577×tamp=0628031734\n\n"
"4. http://www.example.com/trip/tr/public/pl_ds?Id=454354×tamp=06280314546\n\n"
"5. http://www.example.com/trip/tr/public/pl_ds")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html