import re
regex = re.compile(r"199\d{1}|20\d{2}|\d{2}(?![/,\-\w ])", flags=re.MULTILINE)
test_str = ("04/20/2001; 04/20/11; 4/20/09; 4/3/01\n"
"Mar-20-2001; Dec 20, 2009; April 20, 2009; Mar. 20, 2009; Mar 20 2009;\n"
"20 Mar 2001; 20 April 2001; 20 Mar. 2007; 20 March, 2009\n"
"Mar 21th, 2009; Mar 21st, 2009; Feb 22nd, 2002\n"
"Feb 2009; Sep 2009; Nov 2016\n"
"6/2008; 12/2009\n"
"2003; 2012")
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