I’m writing my own minifying tool for practice (regular expresssions practice), but after a few tutorials I’m still not getting it.
For example I’m trying to find and remove all comments from my CSS file and that includes:
-
Single line comments as in
/** single line comment ****/ or
/****single line comment */ and
-
Multi line comments as in
/**** start of comment
.myCssClass
{
font:13pt Arial;}
********* end of comment **/
So far I’m using an expression which can only deal with single line comments as follows
(\/\*.*\*\/)
But what I’m trying to understand about regular expressions is how do I tell the regex engine to span lines as well. I did try this:
(\/\*[.\n]*\*\/)
which doesn’t work at all.
Anyone know where I’m going wrong?
Thanks,
Jacques
If you’re running the match in C#, have you tried RegexOptions?
“Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.”
Also see Strip out C Style Multi-line Comments
EDIT:
OK..looks like an issue w/ the regex. Here is a working example using the regex pattern from http://ostermiller.org/findcomment.html. This guy does a good job deriving the regex, and demonstrating the pitfalls and deficiencies of various approaches. Note: RegexOptions.Multiline/RegexOptions.Singleline does not appear to affect the result.