$string = 'my name is mister smith'; if($string =~ /(mr\.|mister (\w+))/){ # if the string contains either 'mr.' or # 'mister', then a space, then some word chars # matching for instance 'mister jones' but not # 'hey mister!' print "surname is $2"; # $1 contains the whole thing, $2 just the surname }