while ($str =~/authentication ?(\S+)?/g){ $authentication .= $authentiction. $1. " "; #more perlish }