Fix Python – python’s re: return True if string contains regex pattern

Question

Asked By – ghostmansd

I have a regular expression like this:

regexp = u'ba[r|z|d]'

Function must return True if word contains bar, baz or bad.
In short, I need regexp analog for Python’s

'any-string' in 'text'

How can I realize it? Thanks!

Now we will see solution for issue: python’s re: return True if string contains regex pattern


Answer

import re
word = 'fubar'
regexp = re.compile(r'ba[rzd]')
if regexp.search(word):
  print('matched')

This question is answered By – mattbornski

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0