A different approach:
Adding on to @Wiktor's sample string,
x <- "This is it, isn't it (well, yes), and (well, this, that, and this, too). Let's look, does it work?"
Now the magic:
> strsplit(x, ", |(?>\\(.*?\\).*?\\K(, |$))", perl = TRUE)
[[1]]
[1] "This is it"
[2] "isn't it (well, yes)"
[3] "and (well, this, that, and this, too). Let's look"
[4] "does it work?"
So how does , |(?>\\(.*?\\).*?\\K(, |$)) match?
| captures either of the groups on either side, both
- on the left, the string
,
- and on the right,
(?>\\(.*?\\).*?\\K(, |$)):
(?> ... ) sets up an atomic group, which does not allow backtracking to reevaluate what it matches.
- In this case, it looks for an open parenthesis (
\\(),
- then any character (
.) repeated from 0 to infinity times (*), but as few as possible (?), i.e. . is evaluated lazily.
- The previous
. repetition is then limited by the first close parenthesis (\\)),
- followed by another set of any character repeated 0 to as few as possible (
.*?)
- with a
\\K at the end, which throws away the match so far and sets the starting point of a new match.
- The previous
.*? is limited by a capturing group (( ... )) with an | that either
- selects an actual text string,
,,
- or moves
\\K to the end of the line, $, if there are no more commas.
*Whew.*
If my explanation is confusing, see the docs linked above, and check out regex101.com, where you can put in the above regex (single escaped—\—instead of R-style double escaped—\\) and a test string to see what it matches and get an explanation of what it's doing. You'll need to set the g (global) modifier in the box next to the regex box to show all matches and not just the first.
Happy strspliting!