import re
regex = re.compile(r"^(0|\+98)9[0-9]{9}$", flags=re.MULTILINE | re.IGNORECASE)
test_str = ("09156987452\n"
"09908472853\n"
"09153610882365\n"
"09158645287\n"
"+9814756842\n"
"+980915874622\n"
"+989908472853\n"
"+981283547\n"
"9836587498\n"
"+986859554\n"
"+989158497858\n"
"+9812457868\n"
"02154091563214587\n"
"12409156032145\n"
"091596356478\n"
"+982654789652\n"
"023654d\n"
"5588\n"
"879 091586424569\n"
"+9878546\n"
"+9999999999999\n"
"+989153610883\n"
"0915603551555555555\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