Remove multiline C style / * comments * / with Perl regex

How to remove multi-line C style comments like:

/* comments
   comments
   comments
   comments */

      

I can remove comments on the same line, for example /* comments */

using multiple codes provided in other questions.

s#/\*[\s\S]*?\*/##sg;
s#/\*(.*?)\*/##sg;
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse

      

All three regular expressions above do not work with multiline comments. How can they be handled?

+3


source to share


1 answer


I would like to,

perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' file

      



Example:

$ cat fi
/* comments
   comments
   comments
   comments */
bar
$ perl -0777pe 's/\/\*(?:(?!\*\/).)*\*\/\n?//sg' fi
bar

      

+4


source







All Articles