import re
regex = re.compile(r"^(?<nr_classe>(\d{4,}))([\x20\t\-]{1,}|[a-zA-Z]|$)", flags=re.MULTILINE)
test_str = ("250156304 - 6 ANO A INTEGRAL ANUA\n"
"250156445 - 7 ANO A INTEGRAL ANUA\n"
"250794211 - 2ª SERIE A INTEGRAL ANUA\n"
"250794484 - 2ª SERIE B INTEGRAL ANUA\n"
"250795069 - 3ª SERIE A INTEGRAL ANUA\n"
"250795291 - 3ª SERIE B INTEGRAL ANUA\n"
"250797255 - 6 ANO A INTEGRAL ANUA\n"
"250797644 - 7 ANO A INTEGRAL ANUA\n"
"250797800 - 7 ANO B INTEGRAL ANUA\n"
"250798147 - 8 ANO A INTEGRAL ANUA\n"
"250798493 - 9 ANO A INTEGRAL ANUA\n"
"250798725 - 9 ANO B INTEGRAL ANUA\n"
"251327540 - 8 ANO A TARDE ANUA\n"
"251327755 - 8 ANO B TARDE ANUA\n"
"251328951 - 7 ANO A TARDE ANUA\n"
"251329173 - 7 ANO B TARDE ANUA\n"
"251329173 -7 ANO B TARDE ANUA\n"
"251331013 - 6 ANO A TARDE ANUA\n"
"252381546 - 6074 - INFORMATICA PARA INTERNET - 1ª SERIE NT MANHA ANUA")
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