# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?=(?P<LE10>(?:0[0-9]|1[0-9]|2[0-8]|3[0-7]|4[0-6]|5[0-5]|6[0-4]|7[0-3]|8[0-2]|9[0-1])))?(?:(?:X|(?P>LE10))-){9}(?:XXX|XX\d|X?(?P>LE10)|(?:19|28|37|46|55|64|7|3|82|91)[X\d])$"
test_str = ("X-91-55-72-X-X-X-90-71-91X\n"
"X-91-55-72-X-X-X-90-82-XXX\n"
"X-91-55-72-X-X-X-90-82-XX7\n"
"X-91-55-72-X-X-X-90-82-X91\n"
"X-91-55-72-X-X-X-90-82-90\n"
"X-91-55-72-X-X-X-90-82-X91\n"
"X-91-55-72-X-X-X-90-82-917\n"
"X-91-72-X-X-X-90-82-917\n"
" Number of delimiters (8) incorrect\n"
"X-91-55-7-X-X-X-90-82-91X\n"
" 7 in first 9 is invalid\n"
"X-91-55-72-X-X-X-X94-82-91X\n"
" X94 in first 9 is invalid\n"
"X-91-55-72-X-X-X-90-82-X\n"
" last = X is not valid\n"
"X-91-55-72-X-X-X-90-82-XX\n"
" last = XX is not valid\n"
"X-91-55-72-X-X-X-90-82-XXXX\n"
" last = XXXX is not valid \n"
"X-91-55-72-X-X-X-90-82-X92\n"
" last = X92 is not valid\n"
"X-91-55-72-X-X-X-90-82-94\n"
" last = 94 is not valid\n"
"X-91-55-72-X-X-X-90-82-62X\n"
" last = 62X is not valid\n"
"X-91-55-72-X-X-X-90-82-927\n"
" last = 927 is not valid\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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