Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Break a long regex across multiple lines of code, with comments

by davidfilmer (Sexton)
on Sep 22, 2015 at 23:24 UTC ( [id://1142771]=note: print w/replies, xml ) Need Help??


in reply to Re: Break a long regex across multiple lines of code, with comments
in thread Break a long regex across multiple lines of code, with comments

Thank you, stevieb.

>>> You might be inclined to show the regex, a bit of surrounding code, and a sample of your data, as there may be more efficient/cleaner ways to do this instead of using one long regex.

Thanks. Here's my demonstrator program, which works properly (though perhaps not efficiently):

#!/usr/bin/perl use strict; my $string = join ( "\n", <DATA> ); #slurp it all into a string wit +h 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 ============================ la la la System Configuration: Oracle Corporation sun4v SPARC Enterprise T5220 la la 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 la la
OUTPUT
System Configuration: 'Oracle Corporation sun4v SPARCE nterprise T5220 +' Memory Size: '65408' Serial Number: 'FDL10792DE'

Replies are listed 'Best First'.
Re^3: Break a long regex across multiple lines of code, with comments
by Athanasius (Archbishop) on Sep 23, 2015 at 07:59 UTC

    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:

    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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1142771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found