# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
^
(?<born> # BORN part start
(?:(?<o>1)|(?<t>2)|(?<h>3)|(?<f>4)|(?<v>5)|(?<s>6)|(?<n>7)|(?<e>8)|(?<i>9)|(?<z>0)) # Thousands
(?:(?<o2>1)|(?<t2>2)|(?<h2>3)|(?<f2>4)|(?<v2>5)|(?<s2>6)|(?<n2>7)|(?<e2>8)|(?<i2>9)|(?<z2>0)) # Centuries
(?:(?<o3>1)|(?<t3>2)|(?<h3>3)|(?<f3>4)|(?<v3>5)|(?<s3>6)|(?<n3>7)|(?<e3>8)|(?<i3>9)|(?<z3>0)) # Decades
(?:(?<o4>1)|(?<t4>2)|(?<h4>3)|(?<f4>4)|(?<v4>5)|(?<s4>6)|(?<n4>7)|(?<e4>8)|(?<i4>9)|(?<z4>0)) # Last digit
)
,
(?!\k<born>) #Fail if years are identical
(?:
#1st digit is lower
(?:(?(o)0|(?(t)[01]|(?(h)[0-2]|(?(f)[0-3]|(?(v)[0-4]|(?(s)[0-5]|(?(n)[0-6]|(?(e)[0-7]|(?(i)[0-8]|-1)))))))))) \d{3} |
#1st digit is lower or identical
(?:(?(o)[01]|(?(t)[0-2]|(?(h)[0-3]|(?(f)[0-4]|(?(v)[0-5]|(?(s)[0-6]|(?(n)[0-7]|(?(e)[0-8]|(?(i)[0-9]|-1))))))))))
#and 2nd is lower
(?:(?(o2)0|(?(t2)[01]|(?(h2)[0-2]|(?(f2)[0-3]|(?(v2)[0-4]|(?(s2)[0-5]|(?(n2)[0-6]|(?(e2)[0-7]|(?(i2)[0-8]|-1)))))))))) \d{2} |
#1st digit is lower or identical
(?:(?(o)[01]|(?(t)[0-2]|(?(h)[0-3]|(?(f)[0-4]|(?(v)[0-5]|(?(s)[0-6]|(?(n)[0-7]|(?(e)[0-8]|(?(i)[0-9]|-1))))))))))
#and 2nd is lower or identical
(?:(?(o2)[01]|(?(t2)[0-2]|(?(h2)[0-3]|(?(f2)[0-4]|(?(v2)[0-5]|(?(s2)[0-6]|(?(n2)[0-7]|(?(e2)[0-8]|(?(i2)[0-9]|-1))))))))))
#and 3rd is lower
(?:(?(o3)0|(?(t3)[01]|(?(h3)[0-2]|(?(f3)[0-3]|(?(v3)[0-4]|(?(s3)[0-5]|(?(n3)[0-6]|(?(e3)[0-7]|(?(i3)[0-8]|-1)))))))))) \d |
#1st digit is lower or identical
(?:(?(o)[01]|(?(t)[0-2]|(?(h)[0-3]|(?(f)[0-4]|(?(v)[0-5]|(?(s)[0-6]|(?(n)[0-7]|(?(e)[0-8]|(?(i)[0-9]|-1))))))))))
#and 2nd is lower or identical
(?:(?(o2)[01]|(?(t2)[0-2]|(?(h2)[0-3]|(?(f2)[0-4]|(?(v2)[0-5]|(?(s2)[0-6]|(?(n2)[0-7]|(?(e2)[0-8]|(?(i2)[0-9]|-1))))))))))
#and 3rd is lower or identical
(?:(?(o3)[01]|(?(t3)[0-2]|(?(h3)[0-3]|(?(f3)[0-4]|(?(v3)[0-5]|(?(s3)[0-6]|(?(n3)[0-7]|(?(e3)[0-8]|(?(i3)[0-9]|-1))))))))))
#and 4th is lower
(?:(?(o4)0|(?(t4)[01]|(?(h4)[0-2]|(?(f4)[0-3]|(?(v4)[0-4]|(?(s4)[0-5]|(?(n4)[0-6]|(?(e4)[0-7]|(?(i4)[0-8]|-1))))))))))
)
$
"""
test_str = ("1852,1891\n"
"1862,1862\n"
"1945,1944\n"
"1933,1922\n"
"1902,1785\n"
"1923,0923\n\n"
"0001,0999\n"
"0001,0990\n"
"0001,0989\n"
"0001,0988\n"
"0001,0987\n"
"0001,0986")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
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