11

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.

Ben
  • 68,572
  • 20
  • 126
  • 174
pranay reddy
  • 111
  • 1
  • 1
  • 3
  • In fact I doubt that a regular expression beginning by two stars and ending by two stars is a valid one – Xavier Combelle Dec 29 '11 at 13:04
  • As a note to answers just want to add - you need to pay close attention to variable value, as it could be treated as regexp things, giving wrong result after applying this regexp. – demalexx Dec 29 '11 at 13:13
  • [**This question**](http://stackoverflow.com/questions/6930982/variable-inside-python-regex) is similar to yours. It may help you. – Shaowei Ling Aug 22 '14 at 13:35

5 Answers5

16

The usual string formatting way works well

re.search(r'**%s+pattern**' % some_variable, str_for_pattern_match, flags)
lig
  • 3,567
  • 1
  • 24
  • 36
7

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!

Ben
  • 68,572
  • 20
  • 126
  • 174
6

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']
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
5
re.search(r'**{0}+pattern**'.format(variable_name), str_for_pattern_match, flags)

now all your {…} will be interpreted as string format placeholders.

eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 4
    One thing to note, though: if the variable has special metacharacters, they will be taken as is... If it is unwanted, one might want to surround the placeholder with `\Q` and `\E`: `r'\Q{0}\E' – fge Dec 29 '11 at 12:19
2

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)
Paul
  • 813
  • 11
  • 27