regex with python variable code example
Example 1: variable in regex python
import re
s = 'I love books'
var_name = 'love'
result = re.search('(.+)'+var_name+'(.+)',s)
print result
var_name = 'hate'
s2 = 'I hate books'
result = re.search('(.+)'+var_name+'(.+)',s2)
print result
Example 2: regex in python
>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')