I need a regex or something to remove this kind of comments.
/*!
* Foo Bar
*/
I tried with /(\/*!.**\/)/m
but fails. Any Suggestion?
I need a regex or something to remove this kind of comments.
/*!
* Foo Bar
*/
I tried with /(\/*!.**\/)/m
but fails. Any Suggestion?
To do it accurately and efficiently, there is a better regex:
Jeffrey Friedl covers this specific problem at length (using C-comments as an example) in his classic work: Mastering Regular Expressions (3rd Edition). Here is a breakdown of the regex which illustrates the "Unrolling-the-Loop" efficiency technique.
Note that this regex does not need Ruby's 'm' dot-matches-all modifier because it does not use the dot!
Additional: So how much more efficient is this regex over the simpler
/\/\*!.*?\*\//m
expression? Well using the RegexBuddy debugger, I measured how many steps each regex took to match a comment. Here are the results for both matching and non-matching: (For the non-,matching case I simply removed the last/
from the comment)As you can see, the lazy-dot solution (which must backtrack once for each and every character in the comment), is much less efficent. Note also that the efficiency difference is even more pronounced with longer and longer comments.
CAVEAT Note that this regex will fail if the opening delimiter occurs inside a literal string, e.g. "This string has a /*! in it!". To do this correctly with 100% accuracy, you will need fo fully parse the script.