'\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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link or
or How to display code and escape characters
are good places to start.