我正在学习 perl 中的正则表达式,我想要一个类似于下面的函数:
sub RegEx()
{
my $T = "0,1,";
my $T2 = "-0:0-0:1-0:2-0:3-0:4-1:0-1:1-1:2-1:3-";
printf ("T= %s <br>", $T);
printf ("T2 %s <br>", $T2);
my @values = split(',', $T);
foreach my $val (@values) {
printf ("We are at item %s in T <br>", $val);
my $temp = $val .":" . "\(\\d\)\+";
printf ("Rexeg %s <br>",$temp);
@result = split(/$temp/, $T2);
foreach my $val2 (@result) {
printf ("T2- %s <br>", $val2);
}
}
}
并根据索引 ($T) 将 $T2 的值解析为数组
但正在显示以下内容
T= 0,1
T2 -0:0-0:1-0:2-0:3-0:4-1:0-1:1-1:2-1:3-
We are at item 0 in T
Rexeg 0:(\d)+
T2- -
T2- 0
T2- -
T2- 1
T2- -
T2- 2
T2- -
T2- 3
T2- -
T2- 4
T2- -1:0-1:1-1:2-1:3-
We are at item 1 in T
Rexeg 1:(\d)+
T2- -0:0-0:1-0:2-0:3-0:4-
T2- 0
T2- -
T2- 1
T2- -
T2- 2
T2- -
T2- 3
T2- -
请让我知道为什么我仍然看到
T2- -0:0-0:1-0:2-0:3-0:4- 当正则表达式为 1:(\d)+
“——”
作为@results 的输出?