in reply to Re: Find and extract substring(s) within larger string.
in thread Find and extract substring(s) within larger string.

Perl provides the $- and $+ @- and @+ arrays for this purpose (thanks to kcott pointing out that arrays have the @ sigil...):

use warnings; use strict; # 1 2 3 4 # 0123456789012345678901234567890123456789012345678 my $dna = 'xxxxxxpecbcbccrlxxxxxxpeeeerlxxxxxplxxxxxPeRLxxxx'; my $re = qr/p.*?l/i; while ( $dna =~ m[($re)]g ) { print join " " => $1, $-[0], $+[0]-1, $/; }

A correction of -1 is required, otherwise the matches are too long by one as in your code above.

Replies are listed 'Best First'.
Re^3: Find and extract substring(s) within larger string.
by 2teez (Vicar) on Oct 21, 2013 at 07:56 UTC

    Hi hdb,
    A correction of -1 is required, otherwise the matches are too long by one as in your code above.
    You are right... my bad!

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me