title: split a string but ignore separators surrounded by given characters
body: <p>I would like to split a string but only use the separator if it's not surrounded by given sets of characters</p>
  
  <p>current :</p>
  
  <pre><code>strsplit("1 ? 2 ? (3 ? 4) ? {5 ? (6 ? 7)}","\\?")
  #&gt; [[1]]
  #&gt; [1] "1 "   " 2 "  " (3 " " 4) " " {5 " " (6 " " 7)}"
  </code></pre>
  
  <p>expected :</p>
  
  <pre><code>strsplit2 &lt;- function(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE,
                        escape = c("()","{}","[]","''",'""',"%%")){
    # ... 
  }
  strsplit2("1 ? 2 ? (3 ? 4) ? {5 ? (6 ? 7)}","\\?")
  #&gt; [[1]]
  #&gt; [1] "1 "   " 2 "  " (3 ? 4) " " {5 ? (6 ? 7)}"
  </code></pre>
  
  <p>I solved this with some complex parsing but I worry about the performance and wonder if regex can be faster.</p>
  
  <hr>
  
  <p>FYI :</p>
  
  <p>My current solution (not really that relevant to the question) is :</p>
  
  <pre><code>parse_qm_args &lt;- function(x){
    x &lt;- str2lang(x)
    # if single symbol
    if(is.symbol(x)) return(x)
    i &lt;- numeric(0)
    out &lt;- character(0)
    while(identical(x[[c(i,1)]], quote(`?`)) &amp;&amp;
          (!length(i) || length(x[[i]]) == 3)){
      out &lt;- c(x[[c(i,3)]],out)
      i &lt;- c(2, i)
    }
    # if no `?` was found
    if(!length(out)) return(x)
  
    if(length(x[[i]]) == 2) {
      # if we have a unary `?` fetch its arg
      out &lt;-  c(x[[c(i,2)]],out)
    } else {
      # if we have a binary `?` fetch the its first arg
      out &lt;-  c(x[[c(i)]], out)
    }
    out
  }
  </code></pre>