【问题标题】:Quotemeta in perl? How to solve error: "Nested quantifiers in regex; marked by <-- HERE"perl中的引用元?如何解决错误:“正则表达式中的嵌套量词;由 <-- HERE 标记”
【发布时间】:2012-01-20 21:16:07
【问题描述】:

以下是代码:

my $vowels = "[aiou~NFKPQRIJ]";
my @diactok;
for $rx (@tokens) {
    $rx =~ s/.\K/$vowels?/g;
    if ($diac =~ /($rx)/) {
        push @diactok, $diac =~ /$rx/g;
    }
}

来自上一个问题:How do I tokenise a word given tokens that are subsumed incompletely in the word?

除了这个错误之外很好(我确实“使用诊断”):

正则表达式中的嵌套量词;由

Note that the minimal matching quantifiers, *?, +?, and
?? appear to be nested quantifiers, but aren't.  See perlre.

用户代码中未捕获的异常: 正则表达式中的嵌套量词;标记为

第 47 行是这一行:

if ($diac =~ /($rx)/)

我尝试了quotemeta,但没有奏效-也许我用错了?在$diac 中捕获的某些字符串确实具有特殊字符,例如'?''*'

【问题讨论】:

    标签: regex perl


    【解决方案1】:

    图案原本是

    (Al*yn)
    

    你改成

    (A[aiou~NFKPQRIJ]?l[aiou~NFKPQRIJ]?*[aiou~NFKP...
    

    正如 nessage 所说,[aiou~NFKPQRIJ]?* 是错误的。你没有指定你想要什么,所以很难给你一个修复。

    也许你想要

    (A(?:[aiou~NFKPQRIJ]?)l(?:[aiou~NFKPQRIJ]?)*(?:[aiou~NFKP...
    

    如果是这样,请使用

    $rx =~ s/.\K/(?:$vowels?)/g;
    

    也许你想要

    (A(?:[aiou~NFKPQRIJ]?)(?:l[aiou~NFKPQRIJ]?)*(?:[aiou~NFKP...
    

    如果是这样,您需要一个比/./ 更好的正则表达式解析器。

    【讨论】:

      【解决方案2】:

      行:

      $rx =~ s/.\K/$vowels?/g;
      

      是罪魁祸首,如果您确实在@tokens 中有元字符。试试这个:

      $rx =~ s/(.)/ quotemeta($1) . "$vowels?" /eg;
      

      请注意,您不能引用整个正则表达式的元数据,因为您在 $vowels 中有需要的元字符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多