title: Regular Expression Select Comma But Not In Between Parentheses
body: <p>I'm looking to create a function in R that loads the defaults of a given function. To do this, I'm using the <code>args</code> argument on a function and looking to break it down to the defaulted arguments of the function and load those into the global environment. This takes a bit of regular expressions and have bumped into this that I'm having difficulty addressing.</p>
  
  <p>Here is a sample function:</p>
  
  <pre><code>myFunc &lt;- function(a = 1, b = "hello world", c = c("Hello", "World")) {}
  </code></pre>
  
  <p>I've gotten it down to this point using my own functions:</p>
  
  <pre><code>x &lt;- "a = 1, b = \"hello world\", c = c(\"Hello\", \"World\")"
  </code></pre>
  
  <p>However, where I am struggling is on splitting the function arguments up. I wanted to split on a comma, but if you have a function argument that has a comma within the default (like the <code>c</code> argument does), then that causes issues. What I'm thinking is if there is a way to call a regular expression that matches a comma, but not a comma this in between two parentheses, then I could use <code>strsplit</code> with that expression to get what I want.</p>
  
  <p>My attempt to match the case of a comma between two parentheses looks like this:</p>
  
  <pre><code>\\(.*,.*\\)
  </code></pre>
  
  <p>Now, I've looked into how to do what I described above and it seems like a negative look ahead may be what I need, so I've attempted to do something like this.</p>
  
  <pre><code>splitx &lt;- strsplit(x, "(?!\\(.*,.*\\)(,)")
  </code></pre>
  
  <p>But R tells me it is an illegal regular expression. If I set <code>perl = TRUE</code> in the argument, it just returns the same string. Any help here would be greatly appreciated and I hope I've been clear!</p>