How can I strip multiline C comments from a file using Perl?

前端 未结 6 1157
情话喂你
情话喂你 2020-12-03 04:02

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

eg:

                  \" WHOLE         


        
6条回答
  •  不思量自难忘°
    2020-12-03 04:52

    Including tests:

    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\n\ncomment */ code /* comment2 */b"),'a code b');
    

提交回复
热议问题