import re
regex = re.compile(r"(?<type>\w*)\/(?<tree>([\w\-]+\.)+)?(?<subtype>[\w\-]+)(\+(?<suffix>[\w\-\.]+))?(; (?<parameter>[\w+-\.=]+))?")
test_str = ("text/html; charset=UTF-8\n"
"application/x-executable\n"
"application/graphql\n"
"application/javascript\n"
"application/json\n"
"application/ld+json\n"
"application/msword\n"
"application/pdf\n"
"application/sql\n"
"application/vnd.api+json\n"
"application/vnd.ms-excel\n"
"application/vnd.ms-powerpoint\n"
"application/vnd.oasis.opendocument.text\n"
"application/vnd.openxmlformats-officedocument.presentationml.presentation\n"
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document\n"
"application/x-www-form-urlencoded\n"
"application/xml\n"
"application/zip\n"
"application/zstd\n"
"audio/mpeg\n"
"audio/ogg\n"
"image/gif\n"
"image/apng\n"
"image/flif\n"
"image/webp\n"
"image/x-mng\n"
"image/jpeg\n"
"image/png\n"
"multipart/form-data\n"
"text/css\n"
"text/csv\n"
"text/html\n"
"text/php\n"
"text/plain\n"
"text/xml")
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