Using .format to use variable in regex findall object. Python

守給你的承諾、 提交于 2019-12-25 02:03:19

问题


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:


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



来源:https://stackoverflow.com/questions/22673739/using-format-to-use-variable-in-regex-findall-object-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!