import re
regex = re.compile(r"(?<!\d)20\d{6}\D?\d{6}(?!\d)")
test_str = ("a20171217091011 = None instead of 2017-12-17 09:10:11\n"
"b20171217091011 = None instead of 2017-12-17 09:10:11\n"
" 20171217091011 = None instead of 2017-12-17 09:10:11\n"
"-20171217091011 = None instead of 2017-12-17 09:10:11\n"
"_20171217091011 = None instead of 2017-12-17 09:10:11\n"
"aa20171217091011 = None instead of 2017-12-17 09:10:11\n"
"a1-20171217091011 = None instead of 2017-12-17 09:10:11\n"
"123_20171217091011 = None instead of 2017-12-17 09:10:11\n"
"123 20171217091011 = None instead of 2017-12-17 09:10:11\n"
"123=20171217091011 = None instead of 2017-12-17 09:10:11\n"
"201712170910110 = 2017-12-17 09:10:11 instead of None\n"
"a20171217091011a = None instead of 2017-12-17 09:10:11\n"
"(20171217091011) = None instead of 2017-12-17 09:10:11\n"
"a-20171217091011 b = None instead of 2017-12-17 09:10:11\n"
"123(20171217091011)456 = None instead of 2017-12-17 09:10:11\n"
" 20171217091011 = None instead of 2017-12-17 09:10:11\n"
"2017 20171217091011 2017 = None instead of 2017-12-17 09:10:11\n"
"20171218-20171217091011-070809 = 2017-12-18 20:17:12 instead of 2017-12-17 09:10:11\n"
"123(20171217-091011)456 = None instead of 2017-12-17 09:10:11\n"
"a2017(20171217 091011)b = None instead of 2017-12-17 09:10:11\n"
"2017xx(20171217?091011)cc2017 = None instead of 2017-12-17 09:10:11\n"
"A-20171116-080910-20171217091011 = None instead of 2017-11-16 08:09:10")
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