in reply to Regular expression matching when it shouldn't
Maybe I'm not getting what you mean but I think you are not understanding pattern matching correctly.
Can someone tell me why it is matching the . or the - at the end of the line?In both cases, the last character is not matched by the regex!
does A or B depending on whether the pattern matched the string $domain or not. It does not change $domain in any way, however. And your pattern does not have to match the whole string:if ($domain =~ /[A-Z0-9]+[A-Z0-9-]+[A-Z0-9]/i) { # do A } else { # do B }
If you are interested in what was matched, you have to use capturing parentheses like this:# /[A-Z0-9]+[A-Z0-9-]+[A-Z0-9]/i matches someDomain someDomain.. ..someDomain.. _!@#!@#!@#01a!@#$ # and so on
Play around with this code and different pattern. Furthermore include the assertion ^ and $ into the pattern as mentioned by OeufMayo and play some more.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"; }
-- Hofmator
|
|---|