Hello davidfilmer,

Here's my demonstrator program, which works properly

It will work properly only as long as you have no more than one configuration/memory/serial_no dataset in the file. As soon as you add a second set, the regex fails:

#! perl use strict; use warnings; my $string = join("\n", <DATA>); # slurp it all into a string with + newlines my ($configuration, $memory, $serial_number) = $string =~ /System Configuration:\s+([\w\s]*?)\n.*Memory size:\s+( +\d+).*Chassis Serial Number\W+(\w+)/s; print "System Configuration: '$configuration'\n", "Memory Size: '$memory'\n", "Serial Number: '$serial_number'\n\n"; __DATA__ ============================ FW Version 1 ============================ System Configuration: Oracle Corporation sun4v SPARC Enterprise T5220 Memory size: 65408 Megabytes Version ------------------------------------------------------------ Sun System Firmware 7.4.7 2014/01/14 18:48 ====================== System PROM revisions ======================= Version ------------------------------------------------------------ OBP 4.33.6.e 2014/01/14 15:19 Chassis Serial Number --------------------- FDL10792DE ============================ FW Version 2 ============================ System Configuration: Oracle Corporation sun4v SPARC Enterprise T5221 Memory size: 65409 Megabytes Version ------------------------------------------------------------ Sun System Firmware 7.4.7 2014/01/14 18:48 ====================== System PROM revisions ======================= Version ------------------------------------------------------------ OBP 4.33.6.e 2014/01/14 15:19 Chassis Serial Number --------------------- abcdefg

Output:

17:45 >perl 1379_SoPW.pl System Configuration: 'Oracle Corporation sun4v SPARC Enterprise T5220 +' Memory Size: '65409' Serial Number: 'abcdefg' 17:45 >

That’s because there are two occurrences of .* in the regex which are greedy, but need to be made non-greedy: .*?

BTW, why don’t you use warnings? Also, why are you adding an extra newline to the end of each line of input data? Simply joining on the empty string would make $string contain the same data as in the file. But the usual Perl idiom for slurping is this (which is simpler):

my $string = do { local $/; <DATA>; };

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Break a long regex across multiple lines of code, with comments by Athanasius
in thread Break a long regex across multiple lines of code, with comments by davidfilmer

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.