import re
regex = re.compile(r"^(?=.*\b401\b)(?=.*?\be?30(?:th|st)?\b).*", flags=re.MULTILINE)
test_str = ("401 300th st\n"
"40120 30 street\n"
"30 401 plz\n"
"401 30th st\n"
"401 e gibbsborro rd, 305\n"
"401 e 30th street, shelter\n"
"401 east 30st\n"
"401 e30th street, 3\n"
"77-02 30th ave, 3rd fl\n"
"401 e30 st.\n\n")
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