if ($line=~ /(\d{1,3},\d{3,3},\d\d\d K)/){ # e.g. 3,373,560 K $processname=" $`"; # matches before string $memory = $& ; # $& matches the string