简体   繁体   中英

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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