我在多行字符串中有几行数据,其中几行重叠,但我想匹配最长行。以下是示例数据。这有什么正则表达式吗?
This is one two OK.
This is one two OK one three OK.
This is one two OK one three OK one four OK.
This is one two OK one three OK one four OK one five OK
...
O / P现在应该是
这是一个两个OK一个三个OK一个四个OK一个五个OK。
如果我使用量词,它会匹配所有行。输入从This开始,以OK结束,但它应该只匹配最长的一个。请建议我。
答案 0 :(得分:4)
不,你应该使用一个简单的读取循环,比如这个
use strict;
use warnings;
my ($max_len, $longest);
while (<DATA>) {
my $len = length;
($max_len, $longest) = ($len, $_) unless defined $max_len and $len < $max_len;
}
print $max_len, "\n";
print $longest, "\n";
__DATA__
This is one two OK.
This is one two OK one three OK.
This is one two OK one three OK one four OK.
This is one two OK one three OK one four OK one five OK
...
<强>输出强>
57
This is one two OK one three OK one four OK one five OK