I'm trying to extract the arguments from a function-call like string, e.g.:
GetStatus("Param1", "Param2", "ParamWith\"Quotations\"")
I'm useless with regular expressions, but is it possible to match these parameter values in this way, including the escaped quotations?
i.e. Param1, Param2, ParamWith"Quotations"
Better yet, is there a way to extract the function name, and only the arguments if the parentheses and arguments are present?
I'm using C# if that makes a difference.
You can split and trim on the ( to get the function name.
However, grabbing the parameters with \" quotations will be a little more difficult for this reason alone: Can regular expressions be used to match nested patterns?. See L.B's solution, but as said, Regular Expressions weren't invented to deal with nesting.
You asked if it's possible to grab the function name and all of the parameters from the string if there are no quotations surrounding the parameters. I would say this is much more reasonable considering you can avoid the nested quotation marks. I'm sure there are plenty of references on how to parse Unix-like commands (i.e. command -options parameter "parameter with quotes"), but I would approach this using the Regex:
Regex reg = new Regex("\".*?\"")
This will get you all the parameters with quotations in them. Then remove all matches from the original string and split on the ',' for the remaining parameters. This is just how I'm thinking about it, but there might be an easier way if you research it.
To expand on my last point, if you have a string like this "GetStatus(Param1, Param2, ParamsWith\"Quotations\")" I really don't know what to tell you, especially if there are ',' inside the \"Quotations\".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With