如何使用 Perl 从文件中去除多行 C 注释? [英] How can I strip multiline C comments from a file using Perl?

查看:25
本文介绍了如何使用 Perl 从文件中去除多行 C 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能让我用正则表达式去除文件中的多行注释和单行注释?

Can anyone get me with the regular expression to strip multiline comments and single line comments in a file?

例如:

                  " WHOLE "/*...*/" HAS TO BE STRIPED OFF....."

1.   /* comment */
2.   /* comment1 */  code   /* comment2 */ #both /*comment1*/ and /*comment2*/ 
                                             #has to striped off and rest should 
                                                 #remain.
3.   /*.........
       .........
       .........
       ......... */

如果你有这个需要,我真的很感激你......提前致谢.

i realy appreciate you if u do this need.... thanks in advance.

推荐答案

包括测试:

use strict;
use warnings;
use Test::More qw(no_plan);
sub strip_comments {
  my $string=shift;
  $string =~ s#/*.*?*/##sg; #strip multiline C comments
  return $string;
}
is(strip_comments('a/* comment1 */  code   /* comment2 */b'),'a  code   b');
is(strip_comments('a/* comment1 /* comment2 */b'),'ab');
is(strip_comments("a/* comment1

comment */ code /* comment2 */b"),'a code b');

这篇关于如何使用 Perl 从文件中去除多行 C 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆