Python regex: combining re pattern with variable

I would like to combine a python variable and a template. How can i do this?

below is what I would like to do.

re.search(r'**some_variable+pattern**',str_for_pattern_match,flags) 

Thanks for your help.

+6
source share
4 answers

The usual way to format strings works well

 re.search(r'**%s+pattern**' % some_variable, str_for_pattern_match, flags) 
+14
source

Regular expression patterns are not some special thing that Python treats specifically. A pattern is simply a regular string value that the re module will interpret as a pattern.

So the question is not “how can I use a variable in a template?”, But rather “how can I build a string based on a variable?”.

Python docs have a lot of information on how to do this. The documentation on string methods will be especially useful. The most important of these for creating regular expressions is likely to be str.format (as shown in the emumiro answer), which has most of its own , describing how to format basic data types into template strings in almost any way you could would wish.

If you can master the basic operations of strings, then inserting a variable into a regular expression will be the smallest of what you can do!

+5
source
 re.search(r'**{0}+pattern**'.format(variable_name), str_for_pattern_match, flags) 

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

+4
source

You must be careful when inserting strings into a regex pattern.

This is because the string may contain special regular expression characters that 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 regular expression character.

However, this problem can be solved by using the re.escape function in the inserted row:

 >>> r = re.compile(r'%s\w+' % re.escape(t)) >>> r.findall(s) ['*four', '*five'] 
+3
source

Source: https://habr.com/ru/post/904661/


All Articles