in reply to Find and extract substring(s) within larger string.
Hi Anonymous Monk,
I am also considering capturing the beginning and ending index of each match (reading left to right)
You can also do like so, using while loop and index function:
which produces..use warnings; use strict; my $dna = 'xxxxxxpecbcbccrlxxxxxxpeeeerlxxxxxplxxxxxPeRLxxxx'; my $re = qr/p.*?l/i; while ( $dna =~ m[($re)]g ) { my $beg = index( $dna, $1 ); my $len = length($1); print join " " => $1, $beg, $beg + ($len -1), $/; # updated }
#Update: Correction of length of each string longer by one, as rightly pointed out by hdbpecbcbccrl 6 15 peeeerl 22 28 pl 34 35 PeRL 41 44
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find and extract substring(s) within larger string.
by hdb (Monsignor) on Oct 21, 2013 at 07:29 UTC | |
by 2teez (Vicar) on Oct 21, 2013 at 07:56 UTC |