in reply to Match, Capture and get position of multiple patterns in the same string
Using a look-ahead will allow for overlapping patterns. However, @+ contains the same values as corresponding elements of @- when look-aheads are used so sums are necessary.
$ perl -e ' > $str = q{CATINTHEHATWITHABATATINAAAT}; > $rex = qr{(?=([^A]A+T))}; > printf > qq{Found %s, length %d at offset %d to %d\n}, > $1, > length $1, > $-[ 0 ], > $-[ 0 ] + length( $1 ) - 1 > while $str =~ m{$rex}g;' Found CAT, length 3 at offset 0 to 2 Found HAT, length 3 at offset 8 to 10 Found BAT, length 3 at offset 16 to 18 Found TAT, length 3 at offset 18 to 20 Found NAAAT, length 5 at offset 22 to 26 $
I hope this is of interest.
Cheers,
JohnGG
|
|---|