I'm trying to parse input like this:
hostname: / level=incr, 1015 MB 00:00:54 + 26137 files hostname: /boot level=incr, 3731 KB 00:00:13 + 8 files hostname: /directory1 level=incr, 2796 MB 00:01:28 71 + files hostname: /directory2 level=incr, 1369 MB 00:5 +3:51 36 files --->truncated here<--
With this code:
use constant HOST_LINE => qr{ \s+ \w+: \s [\/\w+] \s+ # host, partition level=\w+, \s+ # backup level (\d+) \s+ (\w+) \s+ # amt backed up \d+:\d+:\d+ \s+ # Time (\d+) \s+ files # num. files }xms ; my @lines = <STDIN>; for my $line (@lines) { if ( $line =~ HOST_LINE ) { my ($files, $units, $amt_backed_up) = ($3, $2, $1); print "line - $line, units - $units, amt - $amt_backed_up, fil +es $files\n"; } else { print "here -> $line" } }
and it catches the first line OK and prints the captured values. However, it's not doing that for the other lines else after that, i.e. hits the 'else' and prints "here ->...". I've checked the input file for special characters, of which there are none. In fact, I don't see anything differentiating the format of the lines, i.e. don't know why only the first line is parsed correctly.

Thoughts, anyone?

-- Burvil


In reply to Regexp parses only first line correctly by bowei_99

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.