Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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]
Corion looks at TheDamians new modules. It seems he's written another Perl parser in a regexp and uses it for Keyword::Declare ...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2017-06-23 07:50 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (536 votes). Check out past polls.