I need a function that can test whether a string has any special characters in it. I'm currently using the following function with no luck:
import re
def no_special_characters(s, pat=re.compile('[@_!#$%^&*()<>?/\|}{~:]')):
if pat.match(s):
print(s + " has special characters")
else:
print(s + " has NO special characters")
I get the following results:
no_special_characters('$@') # $@ has special characters
no_special_characters('a$@') # a$@ has NO special characters
no_special_characters('$@a') # $@a has special characters
This doesn't make any sense to me. How can I test for ANY special characters in a string?