Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top