http://www.perlmonks.org?node_id=626508


in reply to I have done this before

'\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; }