# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:(?P<EngDate>[a-zA-Z]+\s)??(?P<Date0>(?<!\.)(?:\d{1,2}(?=[,\s.\/-])|\d{4}))\.??(?(1),?)(?P<d_sep>[\s\.\/-])?(?:(?P<Date1>[^\W\d_]+|\d{1,2})(?(3)(?P=d_sep)))?(?(1)(?:\s\d{2}:\d{2}:\d{2}\s\w+\s)?)(?P<Date2>\d{2}|\d{4})(?!\:|\.|\d))|(?:(?<!\d{2})(?P<Hours>[0-2]?\d)[\:\.](?P<Minutes>[0-6]\d)(?:[\:\.](?P<Seconds>[0-6]\d))?(?:[,\.](?P<SenodsFraction>\d{3}|\d{6}))?(?:\so'clock)?(?:(?:[\.,\s,\w+\s]?(?:(?P<Z>(?:[a-zA-Z]{3,5})|(?:[+-]\d\d?:?[0,3,4]?[0,5]?))|(?P<s>PD|AM|PM|am|pm)))+)?)"
test_str = ("04.march.2012 9:07:00\n"
"1999-03-22T05:06:07.000\n\n"
"20150420\n"
"14 мая 2015 08:30:43\n"
"1999 03 22T05:06:07.000+01:00\n"
" \n"
"22.3.19 5:06:07\n"
"5:06:07 22.3.19\n"
"22. March 1999\n\n\n"
"1999-03-22 5:06:07.PD \n\n"
"March 22, 1999\n"
"March 22 1999\n\n"
"22/3/1999 5:06 AM\n\n"
"Mar 22 05:06:07 CET 1999\n\n"
"Mon Mar 22 05:06:07 CET 1999\n\n\n\n"
" \n"
"Monday, March 22, 1999 5:06:07 o'clock AM CET\n\n"
"22-Mar-1999 5:06:07 AM CET\n\n"
"22-Mar-1999 5:06:07 CET AM\n\n"
"1999-03-22 5.06.07.PD CET\n\n"
"Monday, March 22, 1999 5:06:07 AM CET")
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