English 中文(简体)
I want a small regex that accepts a var(int,enum1,enum2) in C#
原标题:

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!

最佳回答

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.
问题回答

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.

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

^                           #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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签