0

I have a function call of the form:

myFunc(X,Y,Z=c("1","2","3")) {
   .. code ..
}

I'm able to extract the outer arguments declaration as: X,Y,Z=c("1","2","3")

I'm trying to use regex to extract the variable names; however, a simple split on ',' doesn't work as the variable Z has a default value associated with it. What's a regex I can use to get the three arguments (Z can have another regex to separate it from it's default value using the '=' character as a further step later).

Any ideas?

GG_Python
  • 2,967
  • 3
  • 27
  • 41
  • This is not functioning code. Please research *writing R functions*, perhaps from [here](http://www.statmethods.net/management/userfunctions.html) or, from @hadley's [**Adv-R** book](http://adv-r.had.co.nz/Functions.html), a great read. – r2evans May 24 '15 at 15:53
  • oh, ya, i've removed the "r" tag since the question is quite generic and might help in other programming language context, sorry to have imposed on the 'r' forum specifically.. – GG_Python May 24 '15 at 15:55
  • 1
    You will need to look out for opening "things" such as quotes, parens, ticks, brackets, and look for the closing form again before looking for the next comma. Try [1757065](http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes), [3147836](http://stackoverflow.com/questions/3147836/c-sharp-regex-split-commas-outside-quotes), or [9605773](http://stackoverflow.com/questions/9605773/java-regex-split-comma-separated-values-but-ignore-commas-in-quotes). – r2evans May 24 '15 at 15:59
  • 2
    @GG_Python: How about this: `(\w+)(?=(?:,|\s*=))`? [(demo)](https://regex101.com/r/uW6kD8/1) – Amal Murali May 24 '15 at 16:06
  • +1, works great; followup trying to get it to include the actual default argument (namely match Z=c("1","2","3"), not only Z..). Quick fix? Tried to add parenthesis in various locations, but mostly it matched the '=' .. I guess this relates to capturing vs non-capturing group? – GG_Python May 24 '15 at 16:27

0 Answers0