# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?:\((?P<imp>[A-Z])\)\ )?(?P<dued>(?:2[0-1][0-9]{2}\-(?:0[0-9]|1[0-2])\-(?:[0-2][0-9]|3[0-1]))|\?)\ (?:\+(?P<proname>[a-zA-Z0-9]+)(?:\#(?P<pronum>[0-9]+))?\ )?(?:\$(?P<startd>2[0-1][0-9]{2}\-(?:0[0-9]|1[0-2])\-(?:[0-2][0-9]|3[0-1]))(?:\^(?P<repeat>\d*(?:D|W|M|Y)(?:\,\d*(?:D|W|M|Y)+)*))?\ )?(?P<todo>.+?(?=(?:\ (?:\@|\&))|$))(?P<contexts>(?:\ \@\S+)*)(?P<tags>(?:\ \&\S+)*)"
test_str = ("(A) 2016-10-23 +projectB $2016-10-03 to be completed as soon as possible @context_bar\n"
"(A) 2016-11-01 +projectA $2016-09-25 job to complete @context @context_two\n"
"(B) ? +houseworks $2016-10-04^2D,W lavoro importante &TAG_FOO &TAG_BAR\n"
"2016-12-15 +projectA#2 $2016-09-25 second job related to the same project @another_context\n"
"? +houseworks $2016-10-02^3D altro lavoro di casa @context_bar &TAG\n"
"? +projectA#3 $2016-09-25 third job of the same project @foo_context &WAIT\n"
"? +projectA#4 $2016-09-25 fourth job, needs third to be completed first\n"
"2016-11-23 $2015-06-23^4Y another job without project @context_foo @context_bar &TAG_A &TAG_B\n"
"? minimal job like email fra@me.com")
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