in reply to Regular Expn Problem
The problem is that with (?:Other\s+(\d+))? all being optional, and preceded by .+?, the regex doesn't need to match the conditional last element as the preceding element happily matches to the end of the string.
One way to ensure that the last element is matched if it exists, is to force the preceding element .+? to be terminated early if it it does.
$text =~ m[ Total\scount\s+(\d+).+? C\s+(\d+).+? G\s+(\d+) .+?(?=Other|$) (?:Other\s+(\d+))? ]sx;
Using the alternation in the lookahead, will ensure that if the "Other" line exists, the final element of the regex will be forced to match it.
You'll still need to check the last capture for undef to decide whether the "other" line was present or not.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Regular Expn Problem
by TilRMan (Friar) on May 10, 2004 at 08:25 UTC | |
|
Re: Re: Regular Expn Problem
by venkatr_n (Sexton) on May 10, 2004 at 06:05 UTC | |
by BrowserUk (Patriarch) on May 10, 2004 at 06:49 UTC |