简体   繁体   中英

how to use a variable instead of a string inside regex search function in python

I have this regex function to extract a specific word in a string

fileName = re.search(r'path1\w([A-Za-z\d]+)', self.fileList[0]).group(1)

path1 is an actual string

What if I would like to replace it by fileName variable where fileName = "path1"

I tried:

print re.search(r'\w([A-Za-z\d]+)' % fileName, self.fileList[0]).group(1)

I got this error:

TypeError: not all arguments converted during string formatting

Why did I get this error ? how to solve this problem

You need a %s in your regex :

print re.search(r'%s\w([A-Za-z\d]+)' % fileName, self.fileList[0]).group(1)

Or as a more pythoinc and flexible way you can use str.format function :

print re.search(r'{}\w([A-Za-z\d]+)'.format(fileName), self.fileList[0]).group(1)

Note that is second way if you have a list of file names you can loop over them and pass the file names to format .

One should be very careful when interpolating strings into languages like Regex. In this case, you should probably escape the string first:

expression = r'{}\w([A-Za-z\d]+)'.format(re.escape(fileName))
re.search(expression, self.fileList[0]).group(1)

It's maybe also worth noting regex 's named lists:

import regex

expression = regex.compile(r'\L<filename>\w([A-Za-z\d]+)', filename=[fileName])
expression.search(self.fileList[0]).group(1)

This avoids having to regex-escape the literal, and works better if there are multiple options. (Plus regex is tons better anyway, so all the more reason to use it!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM