使用带有/g
标志和\G
锚点的正则表达式执行此操作,但在标量上下文中。这会在最后一个模式匹配之后(或第一个模式匹配的开头)保持字符串中的位置。你可以这样沿着绳子走。获取长度,跳过冒号,然后使用 substr 获取正确数量的字符。您实际上可以分配给pos
,因此为您刚刚提取的字符更新它。redo
直到你没有更多的比赛:
use v5.10.1;
LINE: while( my $line = <DATA> ) {
chomp( $line );
{
say $line;
next LINE unless $line =~ m/\G(\d+):/g; # scalar /g!
say "\t1. pos is ", pos($line);
my( $length, $string ) = ( $1, substr $line, pos($line), $1 );
pos($line) += $length;
say "\t2. pos is ", pos($line);
print "\tFound length $length with [$string]\n";
redo;
}
}
__END__
4:spam6:Roscoe
6:Buster10:green eggs
4:abcd5:123:44:Mimi
注意最后输入行中的边缘情况。那3:
是字符串的一部分,而不是新记录。我的输出是:
4:spam6:Roscoe
1. pos is 2
2. pos is 6
Found length 4 with [spam]
4:spam6:Roscoe
1. pos is 8
2. pos is 14
Found length 6 with [Roscoe]
4:spam6:Roscoe
6:Buster10:green eggs
1. pos is 2
2. pos is 8
Found length 6 with [Buster]
6:Buster10:green eggs
1. pos is 11
2. pos is 21
Found length 10 with [green eggs]
6:Buster10:green eggs
4:abcd5:123:44:Mimi
1. pos is 2
2. pos is 6
Found length 4 with [abcd]
4:abcd5:123:44:Mimi
1. pos is 8
2. pos is 13
Found length 5 with [123:4]
4:abcd5:123:44:Mimi
1. pos is 15
2. pos is 19
Found length 4 with [Mimi]
4:abcd5:123:44:Mimi
我想这可能有一个模块,并且有:Bencode。它做了我所做的。这意味着我做了很多工作都白费。始终首先查看 CPAN。即使您不使用该模块,也可以查看他们的解决方案:)