in reply to Re: Re: Re: Re: Re: nested pattern matching
in thread nested pattern matching
Explanation: $pos contains the minimal offset, $nummatch the minimum number of characters to match. The outer loop iterates through the starting positions, the inner one through the different matching lengths. As the matching is non-greedy, you will get the matches ordered by starting position first, second by length.#!perl use strict; use warnings; my $string = qq[FGTXYZGTFABCGHABCFGTXYZADXYZGTYABC]; my ($pos, $nummatch) = (0, 0); while ($string =~ /^(.{$pos,}?)[XYZ]{3}([A-Za-z]{0,21}?)[ABC]{3}/) { my $tpos = $pos; $pos = length($1) + 1; $nummatch = length($2); while ($string =~ /^(.{$tpos,}?)[XYZ]{3}([A-Za-z]{$nummatch,21}?)[A +BC]{3}/) { print length($1) . ', ' . length($2) . $/; $nummatch = length($2) + 1; } } __OUTPUT__ 3, 3 3, 8 20, 8 25, 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re(6): nested pattern matching
by vinforget (Beadle) on Aug 18, 2003 at 21:26 UTC | |
by CombatSquirrel (Hermit) on Aug 18, 2003 at 22:36 UTC | |
by vinforget (Beadle) on Aug 19, 2003 at 00:19 UTC |