Hackerrank - Smart IDE Extract Comments



 

用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;

 
 

猜你喜欢

转载自yuanhsh.iteye.com/blog/2200519
IDE
今日推荐