How do I remove multi-line C style comments like:
/* comments
comments
comments
comments */
I am able to remove comments in one line like /* comments */ by using several codes provided in other questions.
s#/\*[\s\S]*?\*/##sg;
s#/\*(.*?)\*/##sg;
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse
All three regexes above do not work with multi-line comments. How can they be handled?
I would do like,
perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' file
Example:
$ cat fi
/* comments
comments
comments
comments */
bar
$ perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' fi
bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With