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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I think your regex could be simpler. Apart from that, given what you've said about the task and the data, I'd take a stack approach to handling the input -- something like this:
#!/usr/bin/perl use strict; use warnings; my @stack; my $buffer; my $regex = qr/^.*?(.{5}abc(?:.{10})def.{5})/; my $target_length = 26; # number of characters needed for a match while (<DATA>) { chomp; push @stack, $_; $buffer = join( "", @stack ); if ( $buffer =~ s/$regex// ) { my $target = $1; while ( $target ) { print "Found /$target/ after reading $. lines\n"; $target = ( $buffer =~ s/$regex// ) ? $1 : undef; } @stack = ( $buffer ); } else { while ( length( $buffer ) >= $target_length + length( $stack[0 +] )) { shift @stack; $buffer = join( "", @stack ); warn sprintf( "No match at line %d; stack is %d lines, %d +chars\n", $., scalar @stack, length( $buffer )); } } } __DATA__ sample data with five matches... foo bar 5CHRSabc_TEN_CHRS1def5chrs bax qax moo gar 5 Chrsab c_Ten_Chrs2 de f5Chrsnax zax 5cHrSabc_TeN_ ChRs3def5 chrs etc. and so on and so on and so on ad nausem fivecabc0123456789defmtch4 and then another FIVECabc98765432 +10defMTCH5 and then nothing useful after that forever more up to the end
That will concatenate lines onto a stack, removing line terminations as it goes. As soon as there's a match, it's reported to STDOUT, and the stack is reset to start where the match ended.

If there's no match for an extended stretch of data, the initial line is shifted off the stack so long as the overall length of the remaining lines is enough to hold a match. (I put messages to STDERR to report this, just to see it work.)


In reply to Re: Regular expressions across multiple lines by graff
in thread Regular expressions across multiple lines by abcd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found