import re
regex = re.compile(r"_(1[6-9]|2[0-4])$", flags=re.MULTILINE)
test_str = ("x2016_17_7\n"
"x2017_188\n"
"x2018_199\n"
"x2019_20_10\n"
"x2020_21_11\n"
"x2021_22_12\n"
"x2022_23_13\n"
"x2023_20_10\n"
"x202025_15\n"
"x2016_17_16\n"
"x2017_18_17\n"
"x2018_19_18\n"
"x2019_20_19\n"
"x2020_21_20\n"
"x2021_22_21\n"
"x2022_23_22\n"
"x2023_20_23\n"
"x2020_25_20")
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