# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^#+\s*(.*?)\{#(.*)\}"
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 = 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