in reply to I have done this before
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:# 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"; }
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; }
|
|---|