正则表达式-索引/数组命名捕获组? [英] RegEx - Indexed/Arrayed Named Capture Groups?

查看:106
本文介绍了正则表达式-索引/数组命名捕获组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是某些东西可能以如下格式出现:

I have a situation where something can appear in a format as follows:

---id-H--
Header: data
Another Header: more data
Message: sdasdasdasd
Message: asdasdasdasd
Message: asdasdasd

可能有很多消息,或者只有几条.我宁愿不必走出RegEx,因为我正在使用RegEx解析消息上方的一些标头信息,并且消息和标头是我正在分析的文本的一部分.文本中附带的消息可能很多.

There may be many messages, or just a couple. I'd prefer not having to step outside of RegEx, because I am using the RegEx to parse some header information above the messages and the messages along with the headers are part of the text I am parsing. The messages attached into the text might be many.

我还想使用命名捕获组,所以类似

I would also like to use named capture groups, so something like

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

在匹配项中尽可能多地匹配匹配项,并填充了索引.RegEx中是否存在类似的内容? (我最终将在Perl中使用它.)

where it matches the match as many times as it can with the index filled in. Does anything like this exist in RegEx? (I will eventually be using this in Perl.)

推荐答案

假设每个组之间用空行隔开,这可能会使您更接近:

Assuming each group is separated by an empty line, this might get you closer:

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  

这篇关于正则表达式-索引/数组命名捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆