if ($domain =~ /[A-Z0-9]+[A-Z0-9-]+[A-Z0-9]/i) { # do A } else { # do B } #### # /[A-Z0-9]+[A-Z0-9-]+[A-Z0-9]/i matches someDomain someDomain.. ..someDomain.. _!@#!@#!@#01a!@#$ # and so on #### if ($domain =~ /([A-Z0-9]+[A-Z0-9-]+[A-Z0-9])/i) { # $1 contains what is in the first pair of parentheses print "$1 matched\n\n"; } else { print "$domain did not match\n\n"; }