Reputation: 111
I would like to combine a python variable and pattern. How can I do it?
below is what I would like to do.
re.search(r'**some_variable+pattern**',str_for_pattern_match,flags)
Thanks for your help.
Upvotes: 11
Views: 30002
Reputation: 982
An update for using Python's f-string
syntax (possibly the easiest of them all):
re.search(rf'**{some_variable}pattern**',str_for_pattern_match,flags)
Upvotes: 2
Reputation: 120638
You have to be careful when inserting strings into a regexp pattern.
This is because the string may contain special regexp characters which can lead to errors or give unexpected results.
To give an example:
>>> import re
>>> s = 'one*two*three*four*five'
>>> t = '*f'
>>> r = re.compile(r'%s\w+' % t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 244, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat
This fails because the inserted string contains *
, which is a special regexp character.
However, this problem can be dealt with by using the re.escape
function on the inserted string:
>>> r = re.compile(r'%s\w+' % re.escape(t))
>>> r.findall(s)
['*four', '*five']
Upvotes: 6
Reputation: 71505
Regular expression patterns are not some special extra thing that Python treats specially. A pattern is just a perfectly ordinary string value, which the re
module will interpret as a pattern.
So the question isn't "how can I use a variable in a pattern?", but rather "how can I construct a string based on a variable?".
The Python docs have plenty of information on how to do this. Particularly useful will be the documentation on string methods. The most important of these for the purpose of constructing regular expressions is probably will probably be str.format
(as demonstrated in eumiro's answer), which has a large section of its own describing how to format basic data types into template strings in almost any way you could desire.
If you can master the basic operations on strings, then sticking a variable into a regular expression will be the least of what you can do!
Upvotes: 7
Reputation: 3890
The usual string formatting way works well
re.search(r'**%s+pattern**' % some_variable, str_for_pattern_match, flags)
Upvotes: 16
Reputation: 212955
re.search(r'**{0}+pattern**'.format(variable_name), str_for_pattern_match, flags)
now all your {…}
will be interpreted as string format placeholders.
Upvotes: 5