简体   繁体   English

正则表达式以匹配多行注释

[英]Regular Expression to Match Multiline Comments

I've been trying to match comments in a HTML file using regular expressions and remove them completely through a C#.net (VS2010) solution. 我一直在尝试使用正则表达式匹配HTML文件中的注释,并通过C#.net(VS2010)解决方案将其完全删除。 Here's how comments look like, 这是评论的样子,

/*This flexibility is not available with most other programming languages. E.g. in Java,
the position for \G is remembered by the Matcher object.
The Matcher is strictly associated with a single regular expression and a single subject
string.*/

I did try /\\*.+\\*/ , 我确实尝试过/\\*.+\\*/

str = File.ReadAllText("Test.html");<br />
str = Regex.Replace(str, "/\*.+\*/", "", RegexOptions.Singleline);<br />
File.WriteAllText("Test.html", str);

But they were not working out for me. 但是他们没有为我工作。 I've followed some answers in the forum, but still no luck. 我已经在论坛中关注了一些答案,但是仍然没有运气。

I'd appreciate any help :) 我将不胜感激:)

Thanks... 谢谢...

you have to add an extra layer of escaping in your string literal: 您必须在字符串文字中添加额外的转义层:

str = Regex.Replace(str, "/\*.+\*/", "", RegexOptions.Singleline);

produces /*.+*/ as a pattern because \\ is the escape metacharcter of c# string literals. 产生/*.+*/作为模式,因为\\是c#字符串文字的转义元字符。 you need to specify it using one of the follwing variants ( @ prevents processing of escape sequences, \\\\ should be self-explanatory ...): 您需要使用以下变体之一来指定它( @防止处理转义序列, \\\\应该是不言自明的...):

str = Regex.Replace(str, @"/\*.+\*/", "", RegexOptions.Singleline);

or 要么

str = Regex.Replace(str, "/\\*.+\\*/", "", RegexOptions.Singleline);

要查找/*comments*/ ,请尝试以下正则表达式:

/\/\*.+?\*\//ims

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM