Answers for "python use variable in regex expression"

1

python use variable in regex expression

# Short answer:
# The regex expression is a string which can be built up like any string in
# Python

# Example usage:
import re
your_string = 'Your string to search'
variable = 'ring'
re.search('.+' + variable + '.+', your_string) # or:
re.search('.+{}.+'.format(variable), your_string)
Posted by: Guest on April-21-2022
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
Posted by: Guest on August-20-2020

Code answers related to "python use variable in regex expression"

Python Answers by Framework

Browse Popular Code Answers by Language