用perl来处理正则表达式比较方法,代码很少。
my $str = ""; while(<>) { $str .= $_; } my $out = ""; while($str =~ /(\/\*.*?\*\/|\/\/.*?\n)/gs) { my $comment = $1; if($comment =~ /^\/\*/g) { $comment =~ s/\n[^\S\n]*/\n/g; } if(($out eq "") || ($out =~ /\n$/)) { $out .= $comment; } else { $out .= "\n$comment"; } } print $out;