开发者

Regular expression to identify a function whose general type is <FUNCTION NAME>#(arg1,[arg2],[agr3]....)

Consider the below

Case 1: SDN#(X,)

Case 2: SDN#(X,12)

Case 3: SDN#(34,12)

Case 4: CORR#(X,,)

Case 5: CORR#(X,12,45)

Case 6: CFH#(X,AVG)

These all are some kind of custom functions.

How can I build a regular expression that will identity that the given text satisfies the function requirement

The generalised structure is as <FUNCTION NAME>#(arg1,[arg2],[agr3]....)

e.g. given CORR, or CORR#() it is not a funciton.

Because every function must have atleast one argume开发者_StackOverflow社区nt(which will be alphanumeric).

My approach so far is

Regex r = new Regex(@"([A-Z]+)[#]([A-Z a-z 0-9]+),([A-Z a-z 0-9]+),([A-Z a-z 0-9]+)");
Match m = r.Match(txtFunction.Text);
if (m.Success) MessageBox.Show("OK");
else MessageBox.Show("Not OK");

But it is not working

I am using C#3.0

Thanks


Here's an attempt (even if I don't know if its working in C#):

[A-Z]+#\(([A-Za-z0-9]+,?)*,*\)

Explanation: using CORR#(arg1, arg2, arg3)

[A-Z]+# matches the function name, here CORR#

\( matches the opening parentheses (. Note that I escaped the ( in order to tell the engine not to take it as a grouping construct.

([A-Za-z0-0]+,?)* matches arg1, arg2,. Note that it's a bit hacky because of the ,? which indicates that there might be a , at the end, but it is not necessary.

,* matches "empty commata".

\) matches the closing parentheses. Note that I escaped the ) in order to tell the engine not to take it as a grouping construct.


If you have meaningless spaces in your regex (like you have), you have to specify IgnorePatternWhitespace for the RegexOptions.


I don't know specificaly C# regex flavor, but this one works in PCRE for all your cases :

/^[A-Z]+#\(([a-zA-Z0-9]*,?)*\)$/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜