删除注释并写入没有注释的新文本文件

时间:2013-12-06 10:56:07

标签: comments

我需要创建一个程序,其中输入文件是带有注释的源代码,输出是没有注释的相同代码。

如果它也会消除/ * * /评论,那将是完美的。

1 个答案:

答案 0 :(得分:-1)

要解决您的问题,您必须使用preg_replace

    $notCommentedFile = preg_replace('/#.*/', '', $commentedFile);
    $notCommentedFile = preg_replace('#//.*#', '', $notCommentedFile);
    $notCommentedFile = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#', '', $notCommentedFile);

此3模式将删除对您的文件的评论^^

更新

起坐!没有看到评论X /这适用于php,但我认为像preg_replace这样的java函数存在且正则表达式是通用的:D

这里有一个关于如何在java中使用等效preg_replace的示例 What is the Java equivalent to this preg_replace?

相关问题