in reply to Regex: Matching around a word(s)

I think your problem is that m//g doesn't grab overlapping matches without some help from you. In scalar context, it picks up where it left off.

After a successful match, determine by inspection where the matching term is in the string, and then set pos just after that. Perhaps something like this (untested):

while ($text =~ /$expr/g) { my ($prefix,$term,$suffix) = ($1, $2, $3); print "$prefix<b>$term</b>$suffix\n"; # update pos to just after $term pos() = pos() - length($suffix); }

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: Regex: Matching around a word(s)
by shotgunefx (Parson) on Dec 17, 2005 at 01:06 UTC
    Thanks. On the right track, but it's a bit funky if the matches are right next to each other. I'm going to see if I can't merge close matches.

    -Lee

    perl digital dash (in progress)