Indexed / massive named capture groups?

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

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

There may be many posts or just a couple. I would prefer not to go beyond RegEx, because I use RegEx to parse the header information above the messages, and the messages along with the headers are part of the text I'm processing. There can be many messages embedded in the text.

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

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

where it matches the match as many times as it can when filling out the index. Is there something similar in RegEx? (I will eventually use this in Perl.)

+3
source share
2

, , :

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

Perl, (?<name>...), Perl /(pattern1(pattern2))/ , .

(?<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

.

+2

Source: https://habr.com/ru/post/1749986/


All Articles