regex variable in python 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

# pip install regex
import re
# simple find all
sample = "Nothing lasts... but nothing is lost"
found = re.findall("thing", sample)
print(found)

Example 3: regex in python

>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')