If you want to match across multiple lines, you need to keep multiple lines in memory, but the problem is your while (<INFILE>) only reads one line at a time. So for Athanasius's solution to work, you need to read the entire file into memory ("slurp"), which you can do like this:

open my $ifh, '<', $infile or die "$infile: $!"; my $data = do { local $/; <$ifh> }; close $ifh;

Note that you really should Use strict and warnings, and some other more modern features like lexical filehandles and the three-argument open like I am showing here.

Another approach is to read the file line-by-line, and keep some state in memory. Since you are reading from multiple input files, I suspect you might want to treat them as a continuous stream of input? If so, then for the above approach, you'd have to slurp all the files into memory and concatenate them, which might be a lot, depending on the size of the input files. But that's not necessary for the line-by-line approach:

use warnings; use strict; use File::Glob ':bsd_glob'; my @infiles = sort glob 'SBNUM_*.txt'; my $outfile = 'v2test.txt'; open my $ofh, '>', $outfile or die "$outfile: $!"; my ($prevleft,$prevright); for my $infile (@infiles) { open my $ifh, '<', $infile or die "$infile: $!"; while (<$ifh>) { my ($left,$right) = /^(\d{3}),-->(\d{3}),$/ or die "Couldn't match '$_'"; if (defined $prevleft) { print $ofh "$prevleft,-->$right,\n"; } ($prevleft,$prevright) = ($left,$right); } close $ifh; } close $ofh;

Or, just for fun, the same code as a oneliner:

perl -nle '/^(\d{3}),-->(\d{3}),$/||die;defined$x&&print"$x,-->$2,";$x +=$1' SBNUM_*.txt >v2test.txt

In reply to Re: Multi Line Regex Matches... by haukex
in thread Multi Line Regex Matches... by tj999

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.