i want to find all if statements in C# code which are not followed by brackets. Through regex

雨燕双飞 提交于 2019-12-10 23:16:55

问题


I want to find all if statements and for statements which are not followed by curly brackets '{'. When you write a single line in an if statement you do not mostly enclose it in curly brackets, so I want to find all those if and for statements.

Please help!

Like I want to capture this statement

if (childNode.Name == "B")
    return TokenType.Bold;

but not these kinds

if (childNode.Name == "B")
{
    return TokenType.Bold;
}

I want to do it with regex.


回答1:


Since the underlying mathematics disallow a perfect match, you could go for a good heuristic like "find all 'if' followed by a semicolon without an intervening open brace:

/\<if\>[^{]*;/

where \< and \> are begin-of-word and end-of-word as applicable to your regex dialect. Also take care to ignore all newlines in the input, some regex processors need to be told to do that.


You might want to look at StyleCop too. This is a tool which runs a big set of various checks on your source code. This check is already there.




回答2:


If you want a 100% working solution then a regex will not fit the bill. It's way too easy to trip up a regex with real code. Take for instance the following regex

"^\s*if\W[^{]*\n\s*[^{]"

This will match a good majority of "if" statements that are not surrounded by braces. However it can easily be broken. Take the following samples. The regex will incorrectly flags these as if statements with braces.

Example 1

if ( SomeFunction(() => { return 42; }) ) 

Example 2

/* Big comment
   if ( true ) {
*/

The list goes on and on.

Bottom line, if you want perfection, a regex will not work. If you are satisfied with a less than perfect solution then the above regex should do the trick.




回答3:


You can Google for Finite-State Machine to find out why it is not possible to write a pure regular expression you are asking for.

On the other hand some reg-ex interpreters, like in the Perl language have the possibility to reference previously matched expression, which make it possible to theoretically implement entire C# grammar. Do not follow this idea though ;) David Schmitt idea is nice.



来源:https://stackoverflow.com/questions/438150/i-want-to-find-all-if-statements-in-c-sharp-code-which-are-not-followed-by-brack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!