# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<!--\s*ID\:\s*\d+\s*-->(?=\n*-\s+)"
test_str = ("### Alias\n\n"
"Q: what check to do before creating an alias?\n\n"
"A: check whether any other command exists by that name using `type new-alias-name`\n"
"<!--ID: 1643091253375-->\n\n"
"---\n\n"
"Q: syntax to create an alias?\n\n"
"A: `alias name='string'`\n"
"<!--ID: 1645446702965-->\n\n"
"- no spaces before or after the equal sign\n"
"- single quotes around the command sequence\n"
"![[Pasted image 20220125092954.png]]\n"
"<!--ID: 1643091253408-->\n\n"
"---\n\n"
"### Redirection/Piping\n\n"
"Q: output of `> ls-output.txt`\n\n"
"A: the file will be truncated if it exists or it would be created.\n"
"<!--ID: 1645087988730-->\n\n"
"- Simply using the redirection operator with no command preceding it will truncate an existing file or create a new empty file\n"
"<!--ID: 1643351731943-->\n\n")
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