Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: I have done this before

by saintly (Scribe)
on Jul 13, 2007 at 18:43 UTC ( #626508=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-08-19 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?