import re
regex = re.compile(r"(?i)(\d*\.?\d+)\s*(?:tab|cap|grag|past|sob|comp)", flags=re.MULTILINE)
test_str = (" PRODUCTS\n"
"0 PULSAR AT 20 MG ORAL 30 TAB RECUB\n"
"1 LIPITOR 40 MG 1+1 ORAL 15 TAB\n"
"2 LOFTYL 150 MG ORAL 30 TAB\n"
"3 SOMAZINA 500 MG ORAL 10 COMP RECUB\n"
"4 LOFTYL 30 TAB 150 MG ORAL")
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