import re
regex = re.compile(r"""
^( # Match only leap years
\d\d[2468][048] # multiple of four but not 100
|\d\d[13579][26] # ...
|\d\d0[48] # ...
|[02468][048]00 # multiple of four hundred
|[13579][26]00 # ...
)$
""", flags=re.MULTILINE | re.UNICODE | re.VERBOSE)
test_str = ("^(\n"
"\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]| # multiple of four but not 100\n"
" [02468][048]00 |[13579][26]00 # multiple of four hundred\n"
")\n\n\n"
"// accept: multiple of four ()\n"
"0004\n"
"0008\n"
"0012\n"
"0016\n"
"0020\n"
"0024\n"
"0028\n"
"0032\n"
"0036\n"
"0040\n"
"0044\n"
"0048\n"
"0052\n"
"0056\n"
"0060\n"
"0064\n"
"0068\n"
"0072\n"
"0076\n"
"0080\n"
"0084\n"
"0088\n"
"0092\n"
"0096\n"
"1560\n"
"2004\n"
"2008\n"
"2012\n"
"2016\n"
"2020\n"
"2024\n"
"2028\n"
"2032\n"
"2036\n"
"2040\n"
"2044\n"
"9996\n"
"// accept: Multiple of 400\n"
"0000\n"
"0400\n"
"0800\n"
"1200\n"
"1600\n"
"2000\n"
"2400\n"
"2800\n"
"3200\n"
"3600\n"
"4000\n"
"4400\n"
"4800\n"
"5200\n"
"5600\n"
"6000\n"
"6400\n"
"6800\n"
"7200\n"
"7600\n"
"8000\n"
"8400\n"
"8800\n"
"9200\n"
"9600\n"
"// REJECT: Not a multiple of four\n"
"0101\n"
"0102\n"
"0103\n"
"1537\n"
"1538\n"
"2001\n"
"2002\n"
"2003\n"
"2021\n"
"2022\n"
"2023\n"
"2025\n"
"2026\n"
"2027\n"
"2029\n"
"2030\n"
"2031\n"
"2033\n"
"2034\n"
"2035\n"
"2037\n"
"2038\n"
"2039\n"
"2041\n"
"2042\n"
"2043\n"
"9997\n"
"9998\n"
"9999\n"
"// REJECT: Multiple of 100 but not 400\n"
"0100\n"
"0200\n"
"0300\n"
"0500\n"
"0600\n"
"0700\n"
"0900\n"
"1000\n"
"1100\n"
"1500\n"
"2100\n"
"2200\n"
"2300\n"
"2500\n"
"2600\n"
"2700\n"
"2900\n"
"3000\n"
"3100\n"
"3300\n"
"3400\n"
"3500\n"
"3700\n"
"// REJECT: Negative dates not accepted (malformed)\n"
"-2000\n"
"-0400\n"
"-0100\n"
"-0004\n"
"// REJECT: Out of range date per RFC 3339\n"
"10000\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