title: In Python, how to check if a string only contains certain characters?
body: <p>In Python, how to check if a string only contains certain characters?</p>
  
  <p>I need to check a string containing only a..z, 0..9, and . (period) and no other character.</p>
  
  <p>I could iterate over each character and check the character is a..z or 0..9, or . but that would be slow.</p>
  
  <p>I am not clear now how to do it with a regular expression.</p>
  
  <p>Is this correct? Can you suggest a simpler regular expression or a more efficient approach.</p>
  
  <pre><code>#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='_-/&gt;"!@#12345abcde&lt;')
  
  '''
  Output:
  &gt;&gt;&gt; 
  Valid   : "abcde.1"
  Invalid : "abcde.1#"
  Invalid : "ABCDE.12"
  Invalid : "_-/&gt;"!@#12345abcde&lt;"
  '''
  </code></pre>