0

我需要在一些用 . 分隔的句子中间添加一个字符串\n。例如:

INPUT          OUTPUT
V2+count    -> V2+came+count
V6+num      -> V6+came+num
V10+hi+town -> V10+came+hi+town

N2+hello    -> N2+went+hello
N7+time     -> N7+went+time

到目前为止我写的代码是

if ($new=~/\(came\)\|\(went\)/) {
    my $prev_tag = $`;
    if ($prev_tag5=~ /\(V\d+?\)?\+$/) {
        $new=~ s/\(came\)\|//;
    } else {
        $new=~s/\(went\)\|//;
    }
}
4

1 回答 1

4

我的建议是保持简单,不要尝试同时处理这两种情况。因此,首先将“来”添加到所有匹配的案例中/V\d+/,然后将“去”添加到所有匹配的案例中/N\d+/

$new =~ s/(V\d+\+)/$1came+/;
$new =~ s/(N\d+\+)/$1went+/;
于 2012-09-14T10:28:30.690 回答