Reputation: 777
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
Upvotes: 3
Views: 110
Reputation: 60207
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!)
Upvotes: 0
Reputation: 107347
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
.
Upvotes: 3