Remove multi-line C style /* comments */ using Per

2019-07-04 11:35发布

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?

1条回答
三岁会撩人
2楼-- · 2019-07-04 11:53

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
查看更多
登录 后发表回答