text: Q:
  
  PHP regular expression not working in Python
  
  I have this bit of code:
  import re
  word = 'baz'
  regexp = re.compile(r'a[s|z|d]')
  if regexp.search(word) is not None:
      print 'matched'
  else:
      print 'not matched'
  
  Which I got from SO. That works and prints matched. Now I am trying to get the same thing to work for a different regex which I've got working in PHP.
  This /[a-zA-Z0-9_.-]+$ regex works to filter out these results
  mixed_CASE_word_00008908908808908080 # correctly matches
  word_with_characters_I_dont_want-(1) # correctly does not match
  
  I want to change the above python code to do the same but I'm not familiar with python and I'm struggling. I've so far got:
  import re
  word = 'mixed_CASE_word_00008908908808908080'
  regexp = re.compile(r'/[a-zA-Z0-9_.-]+$')
  if regexp.search(word) is not None:
      print 'matched'
  else:
      print 'not matched'
  
  But this gives me the following result:
  mixed_CASE_word_00008908908808908080 # not matched
  word_with_characters_I_dont_want-(1) # not matched
  
  And I want the code to produce this result
  mixed_CASE_word_00008908908808908080 // matched
  word_with_characters_I_dont_want-(1) // not matched
  
  Any ideas where I am going wrong?
  
  A:
  
  Python regular expressions do not start with / like in some other languages.  So, Python is trying to match a literal / character.  You need to remove this:
  regexp = re.compile(r'[a-zA-Z0-9_.-]+$')
  
  Demo:
  >>> import re
  >>> word = 'mixed_CASE_word_00008908908808908080'
  >>> regexp = re.compile(r'[a-zA-Z0-9_.-]+$')
  >>> regexp.search(word)
  <_sre.SRE_Match object; span=(0, 36), match='mixed_CASE_word_00008908908808908080'>
  >>>
  
  A:
  
  I think you wrongly mean  / as line start. Replace / with ^ (start of the line anchor).
  regexp = re.compile(r'^[a-zA-Z0-9_.-]+$')
  
  And note that [a-zA-Z0-9_] would be written as \w. So you could reduce the above regex to r'^[\w.-]+$'.
  
  