`

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;

 
 

 

  • 大小: 81.4 KB
  • 大小: 119.2 KB
  • 大小: 39.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics