How to parse multiline data records using Perl regular expression?

This line (warn "could not parse block:\n$_\n";) of the attached code gives a warning when the data record contains these lines: Which tells me if there is no line between @ line and window line, the code will not parse.

@dldldldld
window
dfjldfldkldsjfl
difkjdljfdlsfld
fdlsjfdlskfdlsfd
 
But passes when in this line format

@dldldldld
dfdfsfdsfdsf
window
dfjldfldkldsjfl
difkjdljfdlsfld
fdlsjfdlskfdlsfd

I think the solution is to change this line of code meet this new condition but don't know how.

unless (m{^([^\n]+)\n.*?\nwindow[^\n]*\n(.*)$}ms)
#!/usr/bin/perl

use strict;
use warnings;

$/='@';

open 'FH','</home/bwalker/final/real_data_test.txt' or die "Unable to open file: $!";

print "<table border=\"2\">\n";
while (<FH>) {
    s{\s*\@$}{}ms;
    my $file_cnt = scalar split /\n/;
    unless (m{^([^\n]+)\n.*?\nwindow[^\n]*\n(.*)$}ms) {
        warn "could not parse block:\n$_\n";
        next;
    }
    my ($test, @lines) = ($1, split /\n+/, $2);
    my $sub_cnt = 1 + scalar @lines;
    print '<tr align="right" ><td width="50" align="left" rowspan="',scalar(@lines),"\">$test</td><td width=\"400\" align=\"left\">",shift(@lines),"</td></tr>\n",map({
"<tr align=\"right\" ><td width=\"400\" align=\"left\">$_</td></tr>\n" } @lines);
}
print "</table>\n";

Open in new window

areyouready344Asked:
Who is Participating?
 
wilcoxonCommented:
Correct.  It currently will not work without a line in between.  Changing the line to the below should fix the problem:

unless (m{^([^\n]+)\n(?:.*?\n)?window[^\n]*\n(.*)$}ms)
0
 
areyouready344Author Commented:
that's better, thx wilcoxon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.