# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
# Valid quoted metatags & groups must be followed by whitespace or the end of input to be valid; otherwise they are matched as a non-quoted metatag or standard tag respectively
# Both quoted & non-quoted metatags must have a non-empty body (after leading and/or trailing `"` are removed) & a non-empty name; otherwise they are matched as a standard tag
#/\G(?>\s*)(?<token> # Match any leading whitespace to help with \G
# (?<prefix>[-~])?
# (?<body>
# (?<metatag>(?>\w+:(?>"[^\"]+"(?=\s|\z)|#{R_FRAG_TK})))| # Match a metatag (quoted or not)
# (?<group>(?> # Match a single group atomically by:
# (?>\(\s+) # 1. atomically matching a `(` & at least 1 whitespace character
# (?<subquery>(?> # Greedily find one of the following 2 options
# (?!(?<=\s)\)|(?>\s+)\)) # 2. Skip this option if a `)` that's preceded by whitespace is next
# (?> # 3. Matching one of the following 3 options once:
# [-~]?\g<metatag>| # 3A. a metatag (to avoid misinterpreting quoted input as groups)
# [-~]?\g<group>| # 3B. a group (to balance parentheses)
# (?> # 3C. Atomically match either:
# [^\s)]+| # - 1 or more non-whitespace, non-`)` characters greedily, or
# (?<!\s)\)+ # - If not preceded by whitespace, 1 or more `)`
# )* # Match 3C 0 or more times greedily
# )
# (?>(?>\s+)(?!\)))?| # 4. Atomically match all contiguous whitespace (if present). Or;
# (?=(?<=\s)\)|(?>\s+)\)) # 5. Succeed if the prior char was whitespace and the next is a closing parenthesis. Backtracks the parenthesis. Takes advantage of special handling of zero-length matches.
# )+) # If step 5 succeeds, the zero-length match will force the engine to stop trying to match this group.
# (?>\s*)(?<=\s)\) # Check if preceded by whitespace and match the closing parenthesis.
# )(?=\s|\z))|
# (?<tag>\S+) # Match non-whitespace characters (tags)
# ))(?>\s*)/x # Match any trailing whitespace to help with \G
\G(?>\s*)(?<token>(?<prefix>[-~])?(?<body>(?<metatag>(?>\w+:(?>"[^\"]+"(?=\s|\z)|(?>"{0,2})(?!(?<=")(?=\s|\z))\S+)))|(?<group>(?>(?>\(\s+)(?<subquery>(?>(?!(?<=\s)\)|(?>\s+)\))(?>[-~]?\g<metatag>|[-~]?\g<group>|(?>[^\s)]+|(?<!\s)\)+)*)(?>(?>\s+)(?!\)))?|(?=(?<=\s)\)|(?>\s+)\)))+)(?>\s*)(?<=\s)\))(?=\s|\z))|(?<tag>\S+)))(?>\s*)
"""
test_str = "~( jun_kobayashi solo ) ~fav:Somebody ~( jacket_(hotline_miami) duo )"
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
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