gpt4 book ai didi

perl - ?模式?运算符不适用于 Perl 中的“仅匹配一次”

转载 作者:行者123 更新时间:2023-12-01 11:32:46 25 4
gpt4 key购买 nike

我是 Perl 的新手,正在练习一些程序。我遇到了语法错误。请帮助我。

我的 Perl 程序

#!/usr/bin/perl

@list = qw/ food foosball subeo footnote terfoot canic footbridge /;

foreach ( @list ) {
$first = $1 if ?(foo.*)?;
$last = $1 if /(foo.*)/;
}

print "First: $first, Last: $last\n";

输出

syntax error at MatchingOnlyOnce.pl line 9, near "if ?"
Execution of MatchingOnlyOnce.pl aborted due to compilation errors.

perl -v 的输出

This is perl 5, version 24, subversion 1 (v5.24.1) built for MSWin32-x64-multi-t
hread

Copyright 1987-2017, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

最佳答案

使用

$first=$1 if m?(foo.*)?;

?PATTERN? 可以用作 m?PATTERN? 的快捷方式,但您不能再省略匹配运算符的前导 m当您使用 ? 作为分隔符时。


5.14 弃用了从 m?PATTERN?flags 中省略前导 m 的能力。

5.22 移除了从 m?PATTERN?flags 中省略前导 m 的功能。

5.22 和 5.24 的 perlop 列出了 m?PATTERN?flags?PATTERN?flags,但只有前者在这些版本中合法。

5.26 的文档将完全不提及 ?PATTERN?(与 m?PATTERN? 相对)。

关于perl - ?模式?运算符不适用于 Perl 中的“仅匹配一次”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com