Csharp/C#教程:删除C样式多行注释分享


删除C样式多行注释

我有一个C#字符串对象,其中包含generics方法的代码,前面是一些标准的C-Style多行注释。

我想我可以使用System.Text.RegularExpressions删除注释块,但我似乎能够让它工作。

我试过了:

 code = Regex.Replace(code,@"/*.*?*/",""); 

我可以指出正确的方向吗?

使用RegexOptions.Multiline选项参数。

 string output = Regex.Replace(input, pattern, string.Empty, RegexOptions.Multiline); 

完整的例子

 string input = @"this is some stuff right here /* blah blah blah blah blah blah blah blah blah */ and this is more stuff right here."; string pattern = @"/[*][wds]+[*]/"; string output = Regex.Replace(input, pattern, string.Empty, RegexOptions.Multiline); Console.WriteLine(output); 

你在正则表达式中使用反斜杠来转义* ,但你还需要在C#字符串中转义那些反斜杠。

因此, @"/*.*?*/""/\*.*?\*/"

此外,除非您确定输入,否则应使用空格替换注释,而不是空字符串。

你可以试试:

 //*.*?*// 

由于正则表达式中存在一些/,因此最好使用不同的分隔符:

 #/*.*?*/# 

你需要在星星之前逃避你的反斜杠。

上述就是C#学习教程:删除C样式多行注释分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 string str = "hi /* hello */ hi"; str = Regex.Replace(str, "/\*.*?\*/", " "); //str == "hi hi" 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/999601.html

(0)
上一篇 2021年12月26日
下一篇 2021年12月26日

精彩推荐