我正在尝试条件允许或不允许替换的替换。 我有一个弦

$string = "There is <tag1>you can do for it. that dosen't mean <tag2>you are fool.There <tag3>you got it.";

这是两个用于检查条件的哈希。

my %tag = ('tag1' => '<you>', 'tag2'=>'<do>', 'tag3'=>'<no>');
my %data = ('you'=>'<you>');

这是实际替换,其中允许替换不匹配的哈希标签值。

$string =~ s{(?<=<(.*?)>)(you)}{
    if($tag{"$1"} eq $data{"$2"}){next;}
    "I"
}sixe;

在这段代码中,我想用不等于标记中给出的哈希值的条件替换“ you ”。
我可以用next代替吗? 问题是我不能使用\\ g修饰符 在使用next我无法进行下一个替换。 另外,我不能在匹配时修改表达式,并且使用下一个也不会进行第二次匹配,它在那里停止。

===============>>#1 票数:1

您不能在断言后面使用变长的外观。 唯一允许的是特殊的\\K标记。

考虑到这一点,执行此测试的一种方法如下:

use strict;
use warnings;

while (my $string = <DATA>) {
    $string =~ s{<([^>]*)>\K(?!\1)\w+}{I}s;
    print $string;
}

__DATA__
There is <you>you can do for it. that dosen't mean <notyou>you are fool.
There is <you>you can do for it. that dosen't mean <do>you are fool.There <no>you got it.

输出:

There is <you>you can do for it. that dosen't mean <notyou>I are fool.
There is <you>you can do for it. that dosen't mean <do>I are fool.There <no>you got it.

===============>>#2 票数:0 已采纳

很简单,但是我花了两天时间来考虑。 我刚刚写了另一种替代方法,它忽略了之前被next;取消的标记next;

$string = "There is <tag1>you can do for it. that dosen't mean <tag2>you are fool.There <tag3>you got it.";


my %tag = ('tag1' => '<you>', 'tag2'=>'<do>', 'tag3'=>'<no>');
my %data = ('you'=>'<you>');

my $notag;

$string =~ s{(?<=<(.*?)>)(you)}{
    $notag = $2;
    if($tag{"$1"} eq $data{"$2"}){next;}
    "I"
}sie;

$string =~ s{(?<=<(.*?)>)(?<!$notag)(you)}{
   "I"
}sie;

  ask by waghso translate from so

未解决问题?本站智能推荐: