tags:

views:

71

answers:

4

Dear All,

I have never dealt with regular expressions before, and I am facing a problem here. The user has to input a text like Var1(0,enum1,enum2), and I have to check the syntax on the following manner:-

1- make sure Var1 name matches the list of variables I have.. Just a string comparison

2- make sure the parameters are input in sequence.

3- dropping any parameter is allowed. and also no parameter at all is allowed

then I have to take the parameters to fill up some object

will regex do the trick for me, or I have to go for writing a parser !?

Thanks!

+2  A: 

While you can do almost anything with Regex it's hardly ever the best way to do things. I can't come up with a good reason to use a regular expression in your case, except for the sake of learing regular expressions.

Save yourself some headache and write a parser, it will be much easier to maintain when you revisit the code in a couple of months.

Oscar Kilhed
A: 

As you need to validate grammar, my gut reaction would be: That smells like a Parser!

Mereghost
A: 

I wouldn't do this with a regular expression, but here's one that does what you want (I am using general regexp syntax - nothing C# specific).

Assuming that the variables are var1, var2, var_3, and the the options for paramater 1 are par1_1, par1_2 and the options for parameter 2 are par2_1, par2_2, par2_3, etc then the regular expression is:

(var_1|var_2|var_3)\(((par1_1|par1_2|par1_3),)?((par2_1|par2_2,)?((par3_1|par3_2)?\)

Note that:

  • (a|b|c) chooses one of a, b or c
  • \( and \) match parentheses (plain "(" and ")" group)
  • (...)? is optional
  • There is a comma included after each optional parameter block, except the last.
andrew cooke
A: 
^                           #Start of expression
(?<func> [^(]+ )            #All characters before the parenthesis
\(                          #The parenthesis
(        
   (?<param> [^,)]+ )    #A string that ends before the following , or )
   (,| \)$  )            #The following , or ). Make sure that ) only appears at end of line
)*                       #Repeat zero or more times
$                        #End of statement
(?<= \) )                #Make sure that last character was ) and not ,

Use the statement as is with RegexOptions.IgnorePatternWhitespace. match.Groups("func") contains the function name and match.Groups("param").Captures contains the captured parameters.

Validate the actual function names and parameters outside of the regex for simplicity.

Marcus Andrén