Python re.match is not the same as regex_match in C++ std::regex or Java String#matches method, it only searches for a match at the start of the string.
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance.
Thus, to fail a match with your ptr2, the regex should only have a $ (or \Z) anchor at the end if you are using re.match:
See IDEONE demo:
import re
ptr2="hdjfhdjh@@@@"
str1=re.compile(r'[a-zA-Z0-9]+$')
if str1.match(ptr2):
print (" string matches %s",ptr2)
else:
print (" string not matches pattern %s",ptr2)
# => (' string not matches pattern %s', 'hdjfhdjh@@@@')
If you plan to use re.search, you need both ^ and $ anchors to require a full string match:
import re
ptr2="hdjfhdjh@@@@"
str1=re.compile(r'^[a-zA-Z0-9]+$')
if str1.search(ptr2):
print (" string matches %s",ptr2)
else:
print (" string not matches pattern %s",ptr2)
Another demo