marsch has asked for the wisdom of the Perl Monks concerning the following question:
Hello folks,
I have a little problem like that:
In a file are different sections. In these sections are key-value pairs. Every pair can occur arbitrary times. To be more sophisticated, every section can have several names. Sections are divided by one or more empty lines. An example:
{section1}
key1: value1_1
key2: value2_1
key2: value2_2
key3: value3_1
...
{section2}{section3}
key1: value1_1
key2: value2_1
key2: value2_2
key2: value2_3
key3: value3_1
...
My problem is to find out all lines of a certain key in a distinct section, e.g. find all values of key2 in section 2. I tried with and without look-aheads and look-behinds, all I get is either the first or the last value, but never all of them (should be 3 elements):
my @data = $text =~ /(?<=\{section2\})(?:[^\[\]]+\n)*(?:key2:\s+([^\n] ++?)\s*?\n)/sg; my @data = $text =~ /(?:\{section2\}(?:\{section[\d]+\})*\n)(?:\w+?:\s +*(?:.+?)\s*?\n)*?(?:key2:\s+([^\n]+?)\s*?\n)/sg;
UPDATE: I try to tune Net::XWhois (DENIC parser), which uses an engine like
@matches = $self->{ Response } =~ /${$self->{Parser}}{$key}/sg;
to get the entries, so my task is to set up a new parser.
Is it possible at all to solve this problem?
Thanks for any hint
Marco
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stuck with RegEx
by rupesh (Hermit) on Sep 28, 2004 at 09:05 UTC | |
by si_lence (Deacon) on Sep 28, 2004 at 09:18 UTC | |
by marsch (Initiate) on Sep 28, 2004 at 09:13 UTC | |
|
Re: Stuck with RegEx (Parsing whois output)
by cfreak (Chaplain) on Sep 28, 2004 at 13:40 UTC | |
by marsch (Initiate) on Sep 28, 2004 at 14:03 UTC | |
|
Re: Stuck with RegEx
by TedPride (Priest) on Sep 28, 2004 at 09:57 UTC | |
|
Re: Stuck with RegEx
by Jasper (Chaplain) on Sep 28, 2004 at 09:43 UTC | |
|
Re: Stuck with RegEx (Parsing whois output)
by marsch (Initiate) on Sep 29, 2004 at 08:28 UTC |