3

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Barg
  • 2,968
  • 7
  • 26
  • 28
  • Your question is not very clear. You are receiving a `string` that contains `GetStatus("Param1", "Param2", "ParamWith\"Quotations\"")` and you would like to parse that string assuming it's a function call and extract the function name and the parameter values? – Ropstah Apr 13 '12 at 15:48
  • 1
    Please don't prefix your titles with "C#". That's what the tags are for. – John Saunders Apr 13 '12 at 15:55
  • Not to mention explaining that its C# for a third time within the question. – Marcelo Cantos Apr 13 '12 at 15:56
  • @Ropstah, it seems pretty clear to me that that is precisely what the OP is asking for. – Kirk Woll Apr 13 '12 at 15:59
  • @Ropstah: Correct, that is what I'm asking. – Barg Apr 13 '12 at 16:03
  • To clarify, you have a string that looks like this: `"GetStatus(\"Param1\", \"Param2\", \"ParamWith\\\"Quotations\\\"\""` and you need to extract "GetStatus", "Param1", "Param2", and "ParamWith\"Quotations\""? – covertCoder Apr 13 '12 at 16:06
  • @KirkWoll: Can you elaborate on covertCoder some more? :). covertCoder, yes I'm now sure that's it. – Ropstah Apr 13 '12 at 16:09
  • Does you want to extract a variable number of parameters? I assume so. If so, then it's probably better to do it in two passes -- one to extract the complete list of arguments and one to iteratively extract each of the individual parameters from the list of arguments. – Mike Ryan Apr 13 '12 at 16:15

3 Answers3

1

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\".

Community
  • 1
  • 1
covertCoder
  • 446
  • 3
  • 10
0

you can do a String.split on the '(' to get the method name then a split on ',' to split the parameters

EDIT: In lieu of a better solution using regular expressions you could always do the parsing yourself. Assuming a correctly formed string:

  1. split the name from the parameter list by finding the first occurrence of '('.
  2. ignore whitespace until you reach the first character after the '('
  3. If you have a \" read the parameter until you find another \". \" without a preceding \\ indicates the end of the parameter. If the first non-whitespace is not a \" read until you find a whitespace (presumably, depends on what parameters are valid for your string). That's your first parameter.
  4. skip whitespaces until you reach a ',' or a ')'. If ',' then go back to step 2. if ')' you're at the end of your parameter list.
msam
  • 4,259
  • 3
  • 19
  • 32
  • 1
    This will also split if a parameter value contains a `,` – Ropstah Apr 13 '12 at 16:04
  • @msam- Yes, but I also need to escape the quotations, ignore whitespace etc. This is the sort of thing I've seen regular expressions do on their head (because of magic, presumably). – Barg Apr 13 '12 at 16:05
  • You could `trim` the strings and `replace` the "`\\"`" occurrences within the strings... – Ropstah Apr 13 '12 at 16:11
  • However you are still splitting on `,` which also splits `,` occurrences within the parameter values... – Ropstah Apr 13 '12 at 16:11
0

Presuming your params contain only letters,digits and whitespace

string fxn = @"GetStatus(""Param1"", ""Param2"", ""ParamWith\""Quotations\"""")";

var result = Regex.Matches(fxn, @"\""(?<GRP>[\w \\\""]+)\""|(?<GRP>\w+[ ]*)")
    .Cast<Match>()
    .Select(m => m.Groups["GRP"].Value)
    .ToArray();
L.B
  • 114,136
  • 19
  • 178
  • 224