In PCRE a combination of \G
and \K
can be used:
(?:\G(?!^)|\()[^)\s]*\K\s+
\G
continues where the previous match ended
\K
resets beginning of the reported match
[^)\s]
matches any character not in the set
See demo at regex101
The idea is to chain matches to an opening parentheses. The chain-links are either [^)\s]*
or \s+
. To only get spaces \K
is used to reset before. This solution does not require a closing )
.
In other regex flavors that support \G
but not \K
, capturing groups can help out. Eg Search for
(\G(?!^)|\()([^)\s]*)\s+
and replace with captures of the 2 groups (depending on lang: $1$2
or \1\2
) - Regex101 demo
Further there is (*SKIP)(*F)
, a PCRE feature for skipping over certain parts. It is often used together with The Trick. The idea is simple: skip this(*SKIP)(*F)|match that
- Regex101 demo. Also this can be worked around with capture groups. Eg replace ([^)(]*\(|\)[^)(]*)|\s
with$1