Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Multiple regex matches in single string

by hipowls (Curate)
on Apr 27, 2008 at 03:37 UTC ( #683106=note: print w/ replies, xml ) Need Help??


in reply to Multiple regex matches in single string

Given that your data file has thousands of lines you may be better choosing a different approach. I assume that end is always lower case. Set the input record separator to $/ = "\nend\n". Now each read of a file reads in a paragraph terminated by an end by itself on a line. The advantage is that you don't need to read in the whole file to memory and it should scale better.

local $/ = "\nend\n"; while ( my $data = <DATA> ) { $data =~ s/^.*start/start/is; print $data; print '=' x 10, "\n"; } __DATA__ start start start go one end start start start go two end
produces
start go one end ========== start go two end ==========

If my assumption of a lower case end is incorrect then you can achieve the same effect with

my $data = ''; while ( my $line = <DATA> ) { $data .= $line; if ( $line =~ /^end$/i ) { $data =~ s/^.*start/start/is; print $data; print '=' x 10, "\n"; $data = ''; } }


Comment on Re: Multiple regex matches in single string
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-07-05 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls