# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
(?|(?:\G(?!\A)(?<=\|)|^\|\h*\[x\]\h*\|)\h*\K([^|\n]+)(?<=\S)\h*\||\[x]\h*\K([^|\s!]+(?:\h*[^|\s]+)*))|
(?|(?:\G(?!\A)\||^\|\h*\[x]\h*!\h*\|)\h*\K([^|\n]+)(?<=\S)\h*|\[x]\h*!\h*\K([^|\s]+(?:\h*[^|\s]+)*))
"""
test_str = ("Test cases where it should match.\n"
"- [x] Example task. | Task ends. [x] Another task.\n"
"- [x] ! Example task. | This ends. [x] ! Another task.\n"
"- [x] Example task! | Task ends. [x] Another task!\n"
"- [x] ! Example task! | This ends. [x] ! Another task!\n\n"
"This is a sentence. [x] Task is here.\n"
"Other text. Another [x] ! Task is here.\n\n"
"Must not match in the table.\n"
"| | Task name | Plan | Actual | File |\n"
"| :---- | :-------------| :---------: | :---------: | :------------: |\n"
"| [x] | Task example. | 08:00-08:45 | 08:00-09:00 | [[task-one]] |\n"
"| [x] ! | Task example. | 08:00-08:45 | 08:00-09:00 | [[task-one]] |\n\n"
"Groups expected:\n"
"$1: all text after [x]\n"
"$2: all text after [x] !\n\n"
"Regex for each group:\n"
"$1: [^\\|\\s]\\s*\\[x\\]\\s*\\K[^!|\\n]*\n"
"$2: [^\\|\\s]\\s*\\[x\\]\\s*\\!\\s*\\K[^|\\n]*\n\n"
"With the not-capturing group applied:\n"
"$1: (?:[^\\|\\s]\\s*\\[x\\]\\s*\\K[^!|\\n]*)\n"
"$2: (?:[^\\|\\s]\\s*\\[x\\]\\s*\\!\\s*\\K[^|\\n]*)\n\n"
"The combined expression:\n"
"(?:[^\\|\\s]\\s*\\[x\\]\\s*\\K([^!|\\n]*))|(?:[^\\|\\s]\\s*\\[x\\]\\s*\\!\\s*\\K([^|\\n]*))")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
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