正则表达式匹配c#的注释

silymore 2009-04-08 05:28:15
求一个能匹配/**/和//注释的正则表达式,是要用来预处理c#文件,删掉注释

这是测试的例子
int i =0;
//comment 1
string url = "http://www.contoso.com\""; // comment2
string s = @"multi""line
http://www.contoso.com
/*this is not commet*/
";//comment3
/*
comment4*/ i=1;
...全文
1260 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
favouritemie 2011-05-20
  • 打赏
  • 举报
回复
1楼V5~~~~~~~~~~!!!
tedabc 2010-01-20
  • 打赏
  • 举报
回复
(? <=([^"]*"[^"]*){2})//.*
这个正则表达式只能匹配一行引号注释嵌套文本后面的注释.
tedabc 2010-01-19
  • 打赏
  • 举报
回复
[code]
static void Main(string[] args)
{
string atString = @"(?<=([^""]*""[^""]*){2})//.*";
string input = @"""abc//this is not a comment but a string"" //this is a comment";

Match m = Regex.Match(input, atString);
if (m.Success)
{
Console.WriteLine(m.Captures[0]);
}

Console.Read();
}
[\code]
正宗熊猫哥 2009-04-09
  • 打赏
  • 举报
回复
向牛人学习
tianxu0836 2009-04-09
  • 打赏
  • 举报
回复
学习了~~
liang4571231 2009-04-09
  • 打赏
  • 举报
回复
"[/*].*((\r)?+(\n)*(.)*)*[*/]"
/* */的
LemIST 2009-04-09
  • 打赏
  • 举报
回复

Regex singleLineComment = new Regex(@"//(.*)", RegexOptions.Compiled);
Regex multiLineComment = new Regex(@"(?<!/)/\*([^*/]|\*(?!/)|/(?<!\*))*((?=\*/))(\*/)", RegexOptions.Compiled | RegexOptions.Multiline);
Regex RegularString = new Regex("((?<!\\\\)\"([^\"\\\\]|(\\\\.))*\")", RegexOptions.Compiled | RegexOptions.Multiline);
Regex AtString = new Regex("@(\"([^\"]*)\")(\"([^\"]*)\")*", RegexOptions.Multiline | RegexOptions.Compiled);

string text = new StreamReader(@"TextFile1.HTML").ReadToEnd();

Match mat = null;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '\"')
{
mat = RegularString.Match(text, i);
if (mat.Success)
{
i = mat.Index + mat.Length;
}
}
else if (text[i] == '@')
{
mat = AtString.Match(text, i);
if (mat.Success)
{
i = mat.Index + mat.Length;
}
}
else if (text[i] == '/')
{
mat = singleLineComment.Match(text, i);
if (mat.Success)
{
text = text.Remove(mat.Index, mat.Length);
i--;
}
else
{
mat = multiLineComment.Match(text, i);
if (mat.Success)
{
text = text.Remove(mat.Index, mat.Length);
i--;
}
}
}
}
Console.WriteLine(text);
wackyboy 2009-04-09
  • 打赏
  • 举报
回复

Regex.Replace(input,@"((?<!:)//[^\n]+|/\*.*?\*/)","");
genius_tong 2009-04-09
  • 打赏
  • 举报
回复
UP~
gomoku 2009-04-09
  • 打赏
  • 举报
回复

///<comment></comment>
public string GetExactDescription()
{
return "We can use /*...*/ or //... for comments";
}


为什么不用CodeDomProvider而要自己分析呢?
silymore 2009-04-09
  • 打赏
  • 举报
回复
看来比较麻烦,+100分求答案,方法不限于使用正则
silymore 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 LemIST 的回复:]
引用 4 楼 silymore 的回复:
引用 1 楼 LemIST 的回复:
单行注释//(.*)
多行(? <!/)/\*([^*/]|\*(?!/)|/(? <!\*))*((?=\*/))(\*/)
字符串((? <!\\)"([^"\\]|(\\.))*")
需要顺序地识别他们。

首先第一个单行注释就过不去,因为没有考虑到引号
多行的也一样,把/*this is not commet*/ 也摘出来了
字符串那个是什么意思

要想排除字符串和注释之间的嵌套问题,你只能分开写,然后按顺序匹配,没有见到过可以用单个正则…
[/Quote]
你说的顺序是指倒序吗,即使倒序也不行吧

难道先要把字符串拿走,删掉注释然后再把字符串加回来?
LemIST 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 silymore 的回复:]
引用 14 楼 LemIST 的回复:
C# codeRegex singleLineComment=newRegex(@"//(.*)", RegexOptions.Compiled);
Regex multiLineComment=newRegex(@"(? <!/)/\*([^*/]|\*(?!/)|/(? <!\*))*((?=\*/))(\*/)", RegexOptions.Compiled|RegexOptions.Multiline);
Regex RegularString=newRegex("((? <!\\\\)\"([^\"\\\\]|(\\\\.))*\")", RegexOptions.Compiled|RegexOptions.Multiline);
Rege…
[/Quote]
开心:)
silymore 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 LemIST 的回复:]
C# codeRegex singleLineComment=newRegex(@"//(.*)", RegexOptions.Compiled);
Regex multiLineComment=newRegex(@"(?<!/)/\*([^*/]|\*(?!/)|/(?<!\*))*((?=\*/))(\*/)", RegexOptions.Compiled|RegexOptions.Multiline);
Regex RegularString=newRegex("((?<!\\\\)\"([^\"\\\\]|(\\\\.))*\")", RegexOptions.Compiled|RegexOptions.Multiline);
Regex AtString=newRegex("@(\"([^\"]*)\")…
[/Quote]
works perfect
stonehy520 2009-04-08
  • 打赏
  • 举报
回复
没用过,学习
LemIST 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 silymore 的回复:]
引用 1 楼 LemIST 的回复:
单行注释//(.*)
多行(? <!/)/\*([^*/]|\*(?!/)|/(? <!\*))*((?=\*/))(\*/)
字符串((? <!\\)"([^"\\]|(\\.))*")
需要顺序地识别他们。

首先第一个单行注释就过不去,因为没有考虑到引号
多行的也一样,把/*this is not commet*/ 也摘出来了
字符串那个是什么意思
[/Quote]
要想排除字符串和注释之间的嵌套问题,你只能分开写,然后按顺序匹配,没有见到过可以用单个正则可以解决的.如果有的话,我就在这里学习了:)
silymore 2009-04-08
  • 打赏
  • 举报
回复
这是.net的一个正则测试工具,可以试一下 RegEx Tester
http://sourceforge.net/project/showfiles.php?group_id=219960
silymore 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sq_zhuyi 的回复:]
Regex.Replace(code, @"(//[^\n]+?)|(/*.+?*/)", "", RegexOptions.Singleline)
[/Quote]
很奇怪你连语句都写好了为什么不F5一下
ArgumentException:
正在分析“(//[^\n]+?)|(/*.+?*/)”- 嵌套限定符 *。
silymore 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 LemIST 的回复:]
单行注释//(.*)
多行(? <!/)/\*([^*/]|\*(?!/)|/(? <!\*))*((?=\*/))(\*/)
字符串((? <!\\)"([^"\\]|(\\.))*")
需要顺序地识别他们。
[/Quote]
首先第一个单行注释就过不去,因为没有考虑到引号
多行的也一样,把/*this is not commet*/ 也摘出来了
字符串那个是什么意思
路人乙e 2009-04-08
  • 打赏
  • 举报
回复
Regex.Replace(code, @"(//[^\n]+?)|(/*.+?*/)", "", RegexOptions.Singleline)
加载更多回复(2)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧