2010-06-14 85 views
3

我有一個情況可以的東西的格式顯示如下:正則表達式 - 索引/陣列命名的捕獲組?

​​

可能有很多的消息,或只是一對夫婦。我寧願不必走出RegEx之外,因爲我正在使用RegEx來解析消息上方的某些頭信息,並且消息與頭一起是我正在解析的文本的一部分。附在文本中的消息可能很多。

我也想用命名的捕獲組,所以像

Message: (?<Message[index of match]>.+) 

它匹配匹配多次,它可以被填充到索引中。難道這樣的事情在正則表達式存在嗎? (我最終會在Perl使用這個)

+1

你是什麼意思的索引?電話號碼? --- id-H--部分? – Oesor 2010-06-14 19:05:09

+0

我所指的索引既不是。它僅僅是匹配的索引,即匹配1 - 索引0(第一個「消息:」),匹配2 - 索引1(第二個「消息:」),... – 2010-06-14 20:14:00

回答

3

假設每個組由一個空行分隔,這可能讓你更接近:

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 
+0

這是一個特殊的答案,一。我自己真的正在尋找像python m.groups()這樣的提取所有匹配組的東西。 – 2011-05-16 19:21:11

+0

@Benoît:你應該查看perl 5.14 - 它支持命名捕獲組 - http://perldoc.perl.org/perlre.html – Ether 2011-05-24 18:22:55

2

的Perl命名的捕獲緩衝區語法(?<name>...)實際上是對/(pattern1(pattern2))/的Perl語法的替代或替代使用,其中捕獲緩衝區可能是模糊的。

你可能拿到賽(?<name>pattern)則指的是%+%-特殊的散列值的散列形式的%+和%的例子見perlre已命名的捕獲緩衝區的語法和perlvar - 並命名捕獲。

然而,在Perl中有更簡單的解決方案。你可以做一個全局匹配返回一個列表,然後在列表上進行操作。你全部匹配到一個數組中。

這裏有樣本:

foreach my $message ($text=~/^Message: (.*)/gm) { 
    # Process messages... 
} 

my @messages = ($text=~/^Message: (.*)/gm); 
print "The first message is $messages[0]\n"; 

還有更多的方法,但那些2是常見的,Perly

好運。