Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Find and extract substring(s) within larger string.

by hdb (Monsignor)
on Oct 21, 2013 at 07:29 UTC ( [id://1059078]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1059078]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found