Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在perl中用新模式替换匹配的模式?_Regex_Perl - Fatal编程技术网

Regex 如何在perl中用新模式替换匹配的模式?

Regex 如何在perl中用新模式替换匹配的模式?,regex,perl,Regex,Perl,我正在尝试替换与以下模式匹配的字符串: my $string ="+++details of candidate ++name of candidate +age of candidate +++idiot +idi"; 该字符串应替换为: my $string ="hii how fine hii

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

my $string ="+++details of candidate 
           ++name of candidate
           +age of candidate
             +++idiot
                 +idi";
该字符串应替换为:

       my $string ="hii 
            how
           fine
            hii
            fine";
我如何才能做到这一点?

使用

$string =~ s/([^+]*)[+]+((\w+).*)([^ \r\n]*|$)/$1<div class="$3">$2<\/div>/g;
$string =~ s/ class="details"//g;
模式中减少+序列长度的顺序很重要。

可能:

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

如果输入是+++候选名称,那么输出是什么?这看起来像是XY问题。字符串替换以生成HTML?我建议更仔细地研究生成HTML的任务会产生更好的结果。如果单词以++开头,它应该替换为HIII如果它以++开头,它应该替换为howjust hlp me如果句子以++开头,它应该替换为HIII考虑以下句子:请请帮助我回答上面给出的新问题,这是一个真正的黑客方法-
$string =~ s/(\++)([^\+\n]*)/(qw(fine how hii))[length($1)-1]/ge;