Perl的Regexp错误

时间:2014-01-13 15:41:33

标签: regex perl

我有这段代码:

sub makeNonVerbatimSubstitutions {
    my $self = shift;
    my $input = shift;
    $input =~ s/^(\S.*)$/(?{$self -> makeOneNonVerbatimSubstitution ($&)}))/mge;
    return $input;
}

但是Perl不喜欢与regexp一致:

Use of ?PATTERN? without explicit operator is deprecated at RTFWriter.pm line 146.

怎么了?

2 个答案:

答案 0 :(得分:3)

您在替换的右侧使用扩展模式代码,据我所知,这不起作用。而且也不需要。另外,您正尝试使用/e修饰符对其进行评估。你需要的只是:

$input =~ s/^(\S.*)$/$self->makeOneNonVerbatimSubstitution ($&)/mge;

虽然您当然知道使用/m修饰符,如果找到可以作为“换行符”的换行符,这也会在字符串中应用替换。

此外,如此长的子程序名称,名称看起来完全相同,我不会选择。如果需要澄清子例程功能,请改为使用注释。

请注意,正如工具所说,如果您use diagnostics,它会更详细地解释您的错误。这就是我运行类似代码时得到的结果:

Use of ?PATTERN? without explicit operator is deprecated at -e line 1 (#1)
    (D deprecated) You have written something like ?\w?, for a regular
    expression that matches only once.  Starting this term directly with
    the question mark delimiter is now deprecated, so that the question mark
    will be available for use in new operators in the future.  Write m?\w?
    instead, explicitly using the m operator: the question mark delimiter
    still invokes match-once behaviour.

Search pattern not terminated or ternary operator parsed as search pattern at
        -e line 1 (#2)
    (F) The lexer couldn't find the final delimiter of a ?PATTERN?
    construct.

    The question mark is also used as part of the ternary operator (as in
    foo ? 0 : 1) leading to some ambiguous constructions being wrongly
    parsed.  One way to disambiguate the parsing is to put parentheses around
    the conditional expression, i.e. (foo) ? 0 : 1.

这基本上意味着它认为您启动了? ... ?模式,该模式已被弃用,而且,它无法找到结束问号?。这意味着您的解析器无法识别(?...,因为它在此处无效。

答案 1 :(得分:1)

你在行中还有一个比需要多的括号。而不是:

$input =~ s/^(\S.*)$/(?{$self -> makeOneNonVerbatimSubstitution ($&)}))/mge;

应该是:

$input =~ s/^(\S.*)$/(?{$self -> makeOneNonVerbatimSubstitution ($&)})/mge;

(请注意,我删除了最后一个括号)。

相关问题