in reply to Re^2: Match, Capture and get position of multiple patterns in the same string
in thread Match, Capture and get position of multiple patterns in the same string
So if you insist on doing the matching and substitution in two different steps, you have to manually set pos($string) after the substitution:
use strict; use warnings; use 5.010; my $string = "CATINTHEHATWITHABAT"; my $regex = qr{\wAT}i; while ($string =~ m/($regex)/g){ my $match = $1; my $start = $-[0]; my $end = $+[0]; my $hitpos = "$start-$end"; my $lcmatch = lc($match); $string =~ s/$match/$lcmatch/g; # in the next iteration start over where we left off pos($string) = $end; say "$match found at $hitpos "; } say "string: $string";
Since you put parenthesis around the regex, ${^MATCH} can be replaced by the shorter $1, and there's no need for the /p modifier.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Match, Capture and get position of multiple patterns in the same string
by richardwfrancis (Beadle) on Nov 13, 2009 at 08:33 UTC | |
by moritz (Cardinal) on Nov 13, 2009 at 08:57 UTC |