Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Perl一行删除注释,同时删除非注释行_Regex_Perl - Fatal编程技术网

Regex Perl一行删除注释,同时删除非注释行

Regex Perl一行删除注释,同时删除非注释行,regex,perl,Regex,Perl,我使用perl one liner从C文件中删除注释,但它也删除了单词之间带有星号(*)的行,例如 static void * 在我使用的一行程序中: 'perl -0777 -pe ''s{/\*.*?\*/}{}gs'' ' sourceFile ' > ' destFile 谁能推荐一下吗 谢谢也许: “查找/*,向前看,不要看到*/序列并吃下一个字符,重复查找并吃到*/” 但是,请注意,由于注释可以嵌套或用引号括起,/***/,因此您永远无法用正则表达式可靠地解析它们。这个问题

我使用perl one liner从C文件中删除注释,但它也删除了单词之间带有星号(*)的行,例如

static void *
在我使用的一行程序中:

'perl -0777 -pe ''s{/\*.*?\*/}{}gs'' ' sourceFile ' > ' destFile
谁能推荐一下吗

谢谢

也许:

“查找
/*
,向前看,不要看到
*/
序列并吃下一个字符,重复查找并吃到
*/


但是,请注意,由于注释可以嵌套或用引号括起,
/***/
,因此您永远无法用正则表达式可靠地解析它们。

这个问题在perlfaq中得到了回答

“如何使用正则表达式从文件中删除C样式的注释?”


你的正则表达式适合我。我刚刚更正了你的报价,我不知道你是这样使用它,还是复制粘贴的人工制品

perl -0777 -pe 's{/\*.*?\*/}{}gs' sourceFile.c >destFile.c
(我在linux上使用的是
bash


试试它。

试试锚定。
^\*.*?\*
这个特殊的正则表达式有一些问题:perlfaq没有,但可能应该提到使用的选项。使用Regexp::Common几乎总是比使用自己的正则表达式更好的选择。
perl -0777 -pe 's{/\*.*?\*/}{}gs' sourceFile.c >destFile.c