且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Perl 替换中替换匹配之前的所有文本?

更新时间:2022-11-13 23:22:15

sub replacer {

    $line = shift;
    $find = shift;
    $replace = shift;

    $line =~ /([^:]+):/
    if ($1 =~ /$find/) { 
         $line =~ s/([^:]+):/$replace/ ;
         return $line;      
    }
    return ;

}

while (<IN>)
{
    print OUT replacer ($_,"mean","variance");
    print OUT replacer ($_,"pattern","newPattern");
}

我的 perl 有点生疏,所以语法可能不准确.

My perl is a little rusty, so syntax might not be exact.

编辑:把它放在一个函数中.

edit: Put it in a function for ya.