MayuraDeSilva
MayuraDeSilva

Reputation: 13

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

Upvotes: 1

Views: 743

Answers (2)

Raheel Hasan
Raheel Hasan

Reputation: 6023

To find /*comments*/, try this regex:

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

Upvotes: 0

collapsar
collapsar

Reputation: 17238

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);

Upvotes: 1

Related Questions