import re
regex = re.compile(r"\bA(?![^\d\n]*[A-Z]{2})[^\d\n]*(\d+)", flags=re.MULTILINE)
test_str = ("A bcd 1 should match\n"
"Abcd1 should match\n"
"A bcd should not match because there is no number\n"
"A bCD 1 should not match because there is capital C between the A and the number \n"
"A bcd 1 B should match because `1` is before the B\n\n"
"A test this is S 2 test B test 13 A again a test 4\n\n\n"
"A bcd 1 should match and capture 1\n\n"
"Abcd1 should match and capture 1\n\n"
"A bcd should not match because there is no number\n\n"
"A BCd 1 should not match because there is a capital C between the A and the number\n\n"
"A bcd 1 EF should match because 1 is before the EF")
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