W Pythonie, jak sprawdzić, czy ciąg zawiera tylko niektóre znaki?

W Pythonie, jak sprawdzić, czy ciąg zawiera tylko niektóre znaki?

Muszę sprawdzić ciąg zawierający tylko a..z, 0..9 i. (okres) i żadna inna postać.

Mogłem iterować po każdej postaci i sprawdzać, czy znak to a..z lub 0..9, lub. ale to byłoby powolne.

Nie wiem teraz, jak to zrobić za pomocą wyrażenia regularnego.

Czy to jest poprawne? Czy możesz zaproponować prostsze wyrażenie regularne lub bardziej efektywne podejście.

#Valid chars . a-z 0-9 
def check(test_str):
    import re
    #http://docs.python.org/library/re.html
    #re.search returns None if no position in the string matches the pattern
    #pattern to search for any character other then . a-z 0-9
    pattern = r'[^\.a-z0-9]'
    if re.search(pattern, test_str):
        #Character other then . a-z 0-9 was found
        print 'Invalid : %r' % (test_str,)
    else:
        #No character other then . a-z 0-9 was found
        print 'Valid   : %r' % (test_str,)

check(test_str='abcde.1')
check(test_str='abcde.1#')
check(test_str='ABCDE.12')
check(test_str='_-/>"!@#12345abcde<')

'''
Output:
>>> 
Valid   : "abcde.1"
Invalid : "abcde.1#"
Invalid : "ABCDE.12"
Invalid : "_-/>"!@#12345abcde<"
'''

questionAnswers(6)

yourAnswerToTheQuestion