如果它在Comments中,则删除行或字符串 [英] delete rows or string if it is in Comments

查看:76
本文介绍了如果它在Comments中,则删除行或字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除C中有//或/*...*/多行或单行等注释的行#



我所做的是,



 静态  string  StripComments( string 代码)
{ var re = @ (^ \ / \ /.*?$)|(\ /*.*?* \ /);
return Regex.Replace(code,re, $ 1);
}


字符串 s = / * #define abcdef
#define ghijkl * /;


while(s.Contains(
#define))
{s = StripComments(s);}





这是正确的我感谢吗?谢谢

解决方案

)|(\ /*.*?* \ /) ;
return Regex.Replace(code,re,

1);
}


字符串 s = / * #define abcdef
#define ghijkl * /;


while(s.Contains(
#define))
{s = StripComments(s);}





这是正确的我感谢吗?谢谢


如果你正确地将其标记为

- 令牌,你只能以健壮的方式检测评论可以包含注释开始/结束字符序列(字符串,字符的所有瑕疵)

- 不能包含注释开始/结束字符序列的标记(除了上述标记和评论之外的所有标记)

- 行评论

- 阻止评论



您可以查看在C#中学习:字符,字符串,字符串格式,关键字,标识符 [ ^ ]在底部,查看用于使用正则表达式对C#进行标记的红利代码,包括行和块注释,并注意上述令牌。



干杯

Andi


Hi, i want to delete rows which has comments like "//" or "/*...*/" multile line or single line in C#

what i did is,

static string StripComments(string code)
 { var re = @"(^\/\/.*?$)|(\/*.*?*\/)";
 return Regex.Replace(code, re, "$1");
 }

In a Class
string s="/*#define abcdef
            #define ghijkl*/;
             

 while (s.Contains("#define"))
 {s= StripComments(s);}



is it correct what i did? thanks

解决方案

)|(\/*.*?*\/)"; return Regex.Replace(code, re, "


1"); } In a Class string s="/*#define abcdef #define ghijkl*/; while (s.Contains("#define")) {s= StripComments(s);}



is it correct what i did? thanks


You can only detect comments in a robust way if you do properly tokenize into
- tokens that can contain comment start/end character sequences (all flawours of strings, characters)
- tokens that cannot contain comment start/end character sequences (all but the above tokens and but comments)
- line comments
- block comments

You may have a look at Escaping in C#: characters, strings, string formats, keywords, identifiers[^] at the bottom, see the bonus code for tokenizing C# with Regex, including line and block comments, taking in care of the above mentioned tokens.

Cheers
Andi


这篇关于如果它在Comments中,则删除行或字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆