3

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

0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.