import re
regex = re.compile(r"""
# Chunk or note time.
(?<= \| )
(?: \s+) (\() ([0-2][0-9]) (:) ([0-5][0-9]) (\))
(?= \s+ \|)
|
# Revision title.
(?<= \s \| \s)
(\(.+\s\#\d+\))
(?= \s+ \|)
|
# Chunk title.
(?<= \| \s [0-2][0-9] : [0-5][0-9] - [0-2][0-9] : [0-5][0-9] \s \| \s )
(?! \[ | \s* \| )
(?:\s*)? (.+?)
(?= \s+ \|)
|
# Chunk note.
(?<= \s \| \s ) (?! -{2} )
(- .+?)
(?= \s+ \|)
""", flags=re.MULTILINE | re.VERBOSE)
test_str = ("[^\\S\\n] (?:\\s*\\r\\n|\\s*\\r|\\s*\\n)\n\n\n"
"# Time block\n\n"
"| Time | Block |\n"
"| ----------: | :---------------- |\n"
"| (00:00) | (Revision #1) |\n"
"| | |\n"
"| 00:00-00:00 | Chunk |\n"
"| | - Note w/ details |\n"
"| 00:00-00:00 | Some meeting |\n"
"| 11:00-13:00 | Chunk |\n"
"| 13:00 | - Note w/ details |\n"
"| | |\n"
"| (00:00) | (Revision #1) |\n"
"| 00:00-00:00 | Chunk |\n"
"| | - Note w/ details |\n\n"
"## Time Tracking\n\n"
"| Tracker | Task | Backlog |\n"
"| ----------: | :---------------------------------- | :------------ |\n"
"| 08:04-08:53 | [x] * Task for deep work block (#1) | [[wiki.link]] |\n"
"| 09:07-09:56 | | Don't match |\n"
"| 98m | | |\n"
"| | | |\n"
"| 00:00-00:00 | Can Match | |\n"
"| | Don't Match | |\n"
"| 00:00-00:00 | | Don't match |\n\n"
"Group 1: `(` for time\n"
"Group 2: `00` hour (for chunk)\n"
"Group 3: `:` (for chunk)\n"
"Group 4: `00` minute (for chunk)\n"
"Group 5: `)` for time\n"
"Group 6: `(Revision #1)`\n"
"Group 7: `Chunk`or `Some meeting`\n"
"Group 8: `- Note w/ details`\n\n")
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