in reply to Regexes: finding ALL matches (including overlap)
use strict; use warnings; my $count; local $_ = "<Pooh,> said Rabbit kindly, <you haven't any brain> <I kn +ow,> said Pooh humbly."; our $c = 0; / ^ (?: # Outside of brackets [^<] | # Inside of brackets < [^y>]* (?: y (?{ local $c = $c + 1 }) [^y>]* )* >? # Optional in case of unmatched bracket. )* $ (?{ $count = $c }) # Save count. /x; print("$count\n");
Since the above will match every string without ever backtracking, using $c is optional. You can replace (?{ local $c = $c + 1 }) with (?{ $count++ }) and drop (?{ $count = $c }).
Sorry, I don't have any general solutions.
Update: Fixed a bug in the regexp.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regexes: finding ALL matches (including overlap)
by tlm (Prior) on Jun 04, 2005 at 11:40 UTC | |
by ikegami (Patriarch) on Jun 04, 2005 at 16:03 UTC | |
by kaif (Friar) on Jun 06, 2005 at 21:13 UTC |