gpt4 book ai didi

regex - 为什么@+ 和@{^CAPTURE} 的长度不同?

转载 作者:行者123 更新时间:2023-12-03 15:55:20 26 4
gpt4 key购买 nike

我试图了解正则表达式变量是如何工作的,因此我可以在嵌入式代码表达式中保存有效负载中的子匹配位置。根据 perlvar ,数组的正索引对应于 $1、$2、$3 等,但情况似乎并非如此?

#!/usr/bin/perl -w
use v5.28;
use Data::Dumper;

"XY" =~ / ( (.*) (.) (?{
say Dumper { match_end => \@+ };
say Dumper { capture => \@{^CAPTURE} }
}) ) (.)/x;

输出 :
$VAR1 = {
'match_end' => [
2,
undef,
1,
2,
undef
]
};

$VAR1 = {
'capture' => [
undef,
'X',
'Y'
]
};

$VAR1 = {
'match_end' => [
1,
2,
0,
1,
undef
]
};

$VAR1 = {
'capture' => [
'XY',
'',
'X'
]
};

最佳答案

@+数组显然已经在编译时被分配或准备好了

perl -MData::Dump=dd -we'$_=q(abc); / (?{dd @+})  ( (.) )/x'

打印
(0, undef, undef)

(0 for the whole match and an undef for each indicated capture group), while

perl -MData::Dump=dd -we'$_=q(abc); / (?{dd @+})  ( (.) (.) )/x'

打印
(0, undef, undef, undef)

with one more element for one more capture group.

One the other hand, the @{^CAPTURE} is just plain empty until there are actual patterns to capture, as we can see from mob's detailed analysis. This, I'd say, plays well with its name.

After the fact the arrays agree, with that shift of one in indices since @+ also contains (offset for) the whole match, at $+[0].

Another difference is that a trailing failed optional match doesn't get a slot in @{^CAPTURE}

perl -MData::Dump=dd -we'$_=q(abc); /((x)? (.) (x)?)/x; dd @+; dd @{^CAPTURE}'

打印

(1, 1, undef, 1, undef)
("a", undef, "a")

关于regex - 为什么@+ 和@{^CAPTURE} 的长度不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60212797/

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