'\w' doesn't match the '.' or '-' characters, so the regex fails.
It only matches A-Z, a-z, 0-9 and the underscore. If you want to go this route, change at least the 3-8th '\w's to something like
[A-Z0-9\.\-].
For even more security and readability, you can construct your regex in a couple extra stages:
# prepare regex sub-expressions
my $sizerx = '(-|\d+(\.\d+)?[GM]?)'; # Matches '-', or a float + 'M'
+ or 'G' for size
my $loadrx = '(-|\d+\.\d\d)'; # Matches '-' or a float
my $procrx = '(-|\d+)'; # matches '-' or an integer
# Test the line to see if it matches
if( $_ =~ /^(\w+)\s+ # host
(\w+)\s+ # os
$procrx\s+ # nproc
$loadrx\s+ # load
$sizerx\s+ # memtot
$sizerx\s+ # memuse
$sizerx # swapto
\s*$
/x
) {
print "using regex, host is [$1]\n";
}
For speed, you should define the sub-expressions outside the loop. If you plan to work with the various parts, you could immediately assign the matches to named variables and check for them, like so:
my($host,$os,$nproc,$load,$memtot,$memuse,$swapto) = (
$_ =~ /^(\w+)\s+ # host
(\w+)\s+ # os
$procrx\s+ # nproc
$loadrx\s+ # load
$sizerx\s+ # memtot
$sizerx\s+ # memuse
$sizerx # swapto
\s*$
/x
);
if(defined $host) {
print "using regex, host is [$host]\n";
} else {
next;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.