Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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
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 = ''; } }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2016-10-24 04:27 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.