Re: Regexp - alternate match and grouping
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 28 Feb 2006 15:03:59 GMT
Witold Rugowski <rugowski1spm@xxxxxx> wrote in comp.lang.perl.misc:
Paul Lalli wrote:
As opposed to Frankenstein-ing your one single regular expression even
It wont be such a monster ;-)) since changes in format are in header
only, so most of information is taken by not showed part of regexp (PIX
syslog messages).
This case is probably easier to do with many regexps as You suggest
(file format is constant so ifs can recognize right format on the
beginning, and then all can be done without additional ifs), however I
can imagine data format for which would very convenient to do such
alternate grouping.
Is this possible?
If the corresponding parts are in the same sequence in all cases,
maybe. But, if I remember your data right, they aren't. Since
parentheses also count from left to right, you can't capture a
late match in an early capture, so no.
Anyway, when using regular expressions, your first interest (after
making them work) is to keep them manageable, which means keeping them
small. It is just a bad idea to combine regexes without need.
I'd go along these lines: Build a regex for each case that matches
the specific case, and let it capture the relevant parts whichever
way. Then assign the captures to named variables that are listed
in the sequence the specific pattern of captures needs. Here is
an example using simplified data:
while ( <DATA> ) {
my( $animal, $fruit);
( $animal, $fruit) = /^case1\s+(\w+)\s+(\w+)/ or
( $fruit, $animal) = /^case2\s+(\w+)\s+(\w+)/ or next;
print "animal: $animal, fruit: $fruit\n";
}
__DATA__
case1 horse orange
case2 apple cat
case2 banana snake
case1 cow tomato
That way the regexes for different cases stay apart, and you get to
maintain the regex and the corresponding sequence of variables on a
single line.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.
- References:
- Regexp - alternate match and grouping
- From: Witold Rugowski
- Re: Regexp - alternate match and grouping
- From: Witold Rugowski
- Re: Regexp - alternate match and grouping
- From: Paul Lalli
- Re: Regexp - alternate match and grouping
- From: Witold Rugowski
- Regexp - alternate match and grouping
- Prev by Date: Re: HTML/DOM parser
- Next by Date: Re: A Problem With GD
- Previous by thread: Re: Regexp - alternate match and grouping
- Next by thread: Re: Regexp - alternate match and grouping
- Index(es):
Relevant Pages
|
|