in reply to Match, Capture and get position of multiple patterns in the same string
How about this
use strict; use warnings; use 5.010; my $string = "CATATHAT"; my $regex = '\wAT'; say 0..9; #print ruler say $string, "\n"; #print original string while ($string =~ /$regex/gi) { my $match_len = length($&); my $start = length($`); my $end = $start + $match_len - 1; say "match: $& ", "start: $start ", "end: $end"; } --output:-- 0123456789 CATATHAT match: CAT start: 0 end: 2 match: HAT start: 5 end: 7
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Match, Capture and get position of multiple patterns in the same string
by 7stud (Deacon) on Nov 12, 2009 at 15:54 UTC |