Using .format to use variable in regex findall obj

2019-09-01 00:49发布

I saw some examples of this here but it did not answer my question: Python Regular Expression findall with variable

i'm trying to use a variable instead of the 9 but i can't seem to figure it out

value = 9
ORF = re.findall(r'ATG(?:(?!TAA|TAG|TGA)…){  value  ,}?(?:TAA|TAG|TGA)',seq) 
#obviously doesn't work or i wouldn't have made this post

I tried:

value = 9
ORF = re.findall(r'ATG(?:(?!TAA|TAG|TGA)…){  {}  ,}?(?:TAA|TAG|TGA)'.format(value),seq)

but got the error:

ValueError: Single '}' encountered in format string

then I tried:

value = r'{}'.format(9)
ORF = re.findall(r'ATG(?:(?!TAA|TAG|TGA)...){value,}?(?:TAA|TAG|TGA)',seq)

and got no error but it didn't work when i looked downstream

How can I get this variable to work in my regular expression?

1条回答
Rolldiameter
2楼-- · 2019-09-01 01:27

Python is complaining that there are other { and } in your string not used for format, and it doesn't know how to tell the difference. In a format string, braces are escaped by doubling them.

You also need to escape any regex characters in your value.

So perhaps:

ORF = re.findall(
    r'ATG(?:(?!TAA|TAG|TGA)…){{{},}}?(?:TAA|TAG|TGA)'
        .format(re.escape(value)),
    seq)

In a case like this, though, I doubt anyone would fault you for using %-formatting or good old string concatenation instead :)

查看更多
登录 后发表回答