如何用perl中的新模式替换匹配的模式? [英] how to substitute the matched pattern with new pattern in perl?

查看:41
本文介绍了如何用perl中的新模式替换匹配的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换与以下模式匹配的字符串:

I am trying to replace the string which matches the following pattern:

my $string ="+++details of candidate 
           ++name of candidate
           +age of candidate
             +++idiot
                 +idi";

该字符串应替换为:

       my $string ="hii 
            how
           fine
            hii
            fine";

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

也许:

$string =~ s/(\++)([^\+\n]*)/(qw(fine how hii))[length($1)-1]/ge;

这篇关于如何用perl中的新模式替换匹配的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆