#!/usr/bin/perl -w use strict; # test on a string with just hours my $uptime = ' 9:12pm up 2:13, 5 users, load average: 0.84, 0.62, 1.03'; if( $uptime =~ /up\s+((\d+) days,\s+)?(\S+),/ ) { my ($dayup,$timeup) = ( $2,$3); # $1 is the first enclosing parens which we don't want print "timeup is ", defined $dayup ? " $dayup days and " : '', "$timeup hours\n"; } # test again on a string with days $uptime = "6:37PM up 4 days, 2:05, 2 users, load averages: 1.99, 1.65, 1.47"; if( $uptime =~ /up\s+((\d+) days,\s+)?(\S+),/ ) { my ($dayup,$timeup) = ( $2,$3); # $1 is the first enclosing parens which we don't want print "timeup is ", defined $dayup ? " $dayup days and " : '', "$timeup hours\n"; }