$re = '/# 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*)/mx';
$str = '~( jun_kobayashi solo ) ~fav:Somebody ~( jacket_(hotline_miami) duo )';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php