if($line=~/Homo sapiens/) { if($line=~/((NP_|XP_))([^ ]+)/) { $matched=$1 . $2; ...do other stuff... } }