Say you wanted to create a pattern that matches sequences of var consecutive digits. You could do it this way:
p = re.compile(r"\d{"+str(var)+"}")
or this way:
p = re.compile(r"\d{%d}" % var)
But how would you do it using format()?
I tried both:
p = re.compile(r"\d{0}".format(var))
and:
p = re.compile(r"\d{{0}}".format(var))
but none of these worked.
You need to actually have triple { and } - two for the escaped literal braces and one for the placeholder:
In [1]: var = 6
In [2]: r"\d{{{0}}}".format(var)
Out[2]: '\\d{6}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With