gpt4 book ai didi

regex - 使用 Perl 替换运算符保留捕获

转载 作者:行者123 更新时间:2023-12-03 01:00:37 25 4
gpt4 key购买 nike

有人可以解释一下为什么下面的代码...

#!/opt/local/bin/perl
use strict;
use warnings;

my $string;

$string = "\t\t\tEntry";
print "String: >$string<\n";

$string =~ s/^(\t*)//gi;

print "\$1: >$1<\n";
print "String: >$string<\n";
print "\n";

$string = "\t\t\tEntry";

$string =~ s/^(\t*)([^\t]+)/$2/gi;

print "\$1: >$1<\n";
print "String: >$string<\n";
print "\n";

exit 0;

...产生以下输出...

String: >           Entry<
Use of uninitialized value in concatenation (.) or string at ~/sandbox.pl line 12.
$1: ><
String: >Entry<

$1: > <
String: >Entry<

...或更直接地说:为什么第一次替换中的匹配值没有保留在 $1 中?

最佳答案

我在 Perl 5.12 的两个实现上尝试过此操作,但没有遇到问题。 5.8 做到了。

因为有 g 选项,perl 会尝试匹配该模式,直到失败。请参阅下面的调试输出。

所以它在 Perl 5.8 中不起作用,但是这个可以:

my $c1;
$string =~ s/^(\t*)/$c1=$1;''/ge;

因此每次匹配时,都会将其保存到 $c1

这就是 use re 'debug' 告诉我的:

Compiling REx `^(\t*)'
size 9 Got 76 bytes for offset annotations.
first at 2
1: BOL(2)
2: OPEN1(4)
4: STAR(7)
5: EXACT <\t>(0)
7: CLOSE1(9)
9: END(0)
anchored(BOL) minlen 0
Offsets: [9]
1[1] 2[1] 0[0] 5[1] 3[1] 0[0] 6[1] 0[0] 7[0]
Compiling REx `^(\t*)([^\t]+)'
size 25 Got 204 bytes for offset annotations.
first at 2
1: BOL(2)
2: OPEN1(4)
4: STAR(7)
5: EXACTF <\t>(0)
7: CLOSE1(9)
9: OPEN2(11)
11: PLUS(23)
12: ANYOF[\0-\10\12-\377{unicode_all}](0)
23: CLOSE2(25)
25: END(0)
anchored(BOL) minlen 1
Offsets: [25]
1[1] 2[1] 0[0] 5[1] 3[1] 0[0] 6[1] 0[0] 7[1] 0[0] 13[1] 8[5] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 14[1] 0[0] 15[0]
String: > Entry<
Matching REx `^(\t*)' against ` Entry'
Setting an EVAL scope, savestack=5
0 <> < Entry> | 1: BOL
0 <> < Entry> | 2: OPEN1
0 <> < Entry> | 4: STAR
EXACT <\t> can match 3 times out of 2147483647...
Setting an EVAL scope, savestack=5
3 < > <Entry> | 7: CLOSE1
3 < > <Entry> | 9: END
Match successful!
match pos=0
Use of uninitialized value in substitution iterator at - line 11.
Matching REx `^(\t*)' against `Entry'
Setting an EVAL scope, savestack=5
3 < > <Entry> | 1: BOL
failed...
Match failed
Freeing REx: `"^(\\t*)"'
Freeing REx: `"^(\\t*)([^\\t]+)"'

因为您试图匹配行开头的空格,所以您既不需要 g 也不需要 i。因此,您可能正在尝试做其他事情。

关于regex - 使用 Perl 替换运算符保留捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5476771/

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