import re
regex = re.compile(r"^#+\s*(.*?)\{#(.*)\}", flags=re.MULTILINE)
test_str = ("# Scoring import {#calculate-score}\n\n"
"Loads Scoring*csv files from the WebDAV and import them locally.\n"
"While processing and when finished, the file gets renamed on the WebDAV.\n\n"
"# Response export {#unsubscribe-export}\n\n"
"Exports unsubscribes, bounces and complaints and stores the export files \n"
"on an SFTP of the customer.\n\n"
"# Campaign uploader {#15mins-campaign-uploader}\n\n"
"Fetches a ZIP from the customers SFTP. \n"
"This ZIP must contain a JSON and a HTML. Images must be stored in a `img` \n"
"folder. Size limit for an image is 1 MB. Images will be uploaded to the \n"
"Suite media library. Campaign meta data will be read from the JSON.\n\n"
"**This process is currently deactivated** \n\n\n"
"# ecard invite a friend landing page {#recommendation-form}\n\n"
"A form to invite friends. The friends data will be stored in the Suite account.\n\n"
"# Pretrip form {#pretrip-information-form}\n\n"
"Asking the end user to provide more more data before they arrive.\n\n"
"# Signup pages and preference center {#preference-center}\n\n"
"Pages to subscribe and to manage personal data.")
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