CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
The best way to return locations of regex?

 



haha
New User

Dec 19, 2008, 9:00 AM

Post #1 of 4 (3358 views)
The best way to return locations of regex? Can't Post

Hi folks,

What is the best method to return all locations for pattern matching? e.g. I have m/somename/g, this will give me all occurrences of pattern "somename", but how do I get the locations of the pattern within the string? Thx!


FishMonger
Veteran / Moderator

Dec 19, 2008, 9:45 AM

Post #2 of 4 (3357 views)
Re: [haha] The best way to return locations of regex? [In reply to] Can't Post

perldoc -f index


haha
New User

Dec 19, 2008, 9:57 AM

Post #3 of 4 (3355 views)
Re: [FishMonger] The best way to return locations of regex? [In reply to] Can't Post

Hi, Fish,

Thx! I know what "index" does but it seems it doesn't solve my problem here. I can use regex to find the patterns first, then find their locations with "index". But that requires two scans of the string. This seems a bit suboptimal. Is there any way to scan once and generate the information I want?


KevinR
Veteran


Dec 19, 2008, 10:30 AM

Post #4 of 4 (3353 views)
Re: [haha] The best way to return locations of regex? [In reply to] Can't Post


Code
$var = 'somenamefoobarsomenametestfoosomefoonamefoosomenametest'; 
push @pos,$-[0] while ($var =~ /somename/g);
print "@pos";

-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives