假設每個組由一個空行分隔,這可能讓你更接近:
use strict;
use warnings;
# use two lines as the "line" separator
local $/ = "\n\n";
while (my $line = <DATA>)
{
my ($id) = ($line =~ /^---id-(\d+)--$/m);
my @messages = ($line =~ /^Message: (.*)$/mg);
print "On line $id, found these messages: ", join(', ', @messages), "\n";
}
__DATA__
---id-1--
Header: data
Another Header: more data
Message: sdasdasdasd
Message: asdasdasdasd
Message: asdasdasd
---id-2--
Header: data2
Another Header: stuff
Message: more message
Message: another message
Message: YAM
運行提供了:
On line 1, found these messages: sdasdasdasd, asdasdasdasd, asdasdasd
On line 2, found these messages: more message, another message, YAM
你是什麼意思的索引?電話號碼? --- id-H--部分? – Oesor 2010-06-14 19:05:09
我所指的索引既不是。它僅僅是匹配的索引,即匹配1 - 索引0(第一個「消息:」),匹配2 - 索引1(第二個「消息:」),... – 2010-06-14 20:14:00