正则表达式以匹配C#代码 [英] Regex to match C# code
问题描述
我正在编写一个小的工具来给我一些代码统计信息.我需要的大部分内容都是书面的,但我仍然需要确定注释掉的代码行.识别注释并不难,但是我特别想将真正的注释与旧注释掉的代码分开.
我正在按照
的思路
正则表达式ccRegex =新正则表达式(C#pattern,RegexOptions.Ignorecase);
IEnumerable< string> commentedCode = linesInFile.Where(l => ccregex.IsMatch(l);
linesInFile是从源文件读取的文本行的字符串数组.
问题是:字符串"C#pattern"应该是什么?
可以用一个正则表达式来完成吗?
TIA
I am writing a small tool to give me some code stats. Most of what I need is written, but I still need to identify commented-out lines of code. Identifying comments is not hard, but I specifically want to separate genuine comments from old commented out code.
I''m thinking along the lines of
Regex ccRegex = new Regex(C#pattern, RegexOptions.Ignorecase);
IEnumerable<string> commentedCode = linesInFile.Where(l => ccregex.IsMatch(l);
linesInFile is a string array of the lines of text read from the source file.
The question is: what should the string ''C#pattern'' be?
Can this be done with a single Regex?
TIA
推荐答案
可以使用一个正则表达式来完成吗?"
编号
从理论上讲,可以扩展一点.但是实际上,正则表达式太复杂了,以至于花费更多的时间进行开发和调试.考虑一下:
"Can this be done with a single Regex?"
No.
To expand a little, in theory it could be done. But in practice the regex would be so complex that it would take more time to develop and debug than it is worth. Think about it:
// Console.WriteLine
这是评论吗?还是注释掉了代码?
如果我们看下面的行
Is that a comment? Or commented out code?
If we look at the following line
// Console.WriteLine
// ("TestString");
然后是的,它被注释掉了代码.
如果我们看下面的另一行:
Then yes, it is commented out code.
If we look at a different following line:
// Console.WriteLine
// Prints a message on the console
那就不是了.
现在,编写一个适用于这两个的正则表达式.现在,将"Console"替换为"MyClass",并编写一个也可以处理的内容...
Then it isn''t.
Now, write a regex that works for those two. Now replace "Console" with "MyClass" and write one that handles that as well...
这篇关于正则表达式以匹配C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!