-2

I have multiple files of the format myfilexyz-200407171758.tar.gz

(myfilexyz)-(200407171758).tar.gz

Group1 is a variable.
Group2 can be of 12 to 14 digits.

Using variable substitution, I can get this working

r = re.compile('(%s)-(\d){12,13}.tar.gz' %myvar)



But if I were to try the newer format method, I get into trouble

r = re.compile('({})-(\d){12,14}.tar.gz'.format(myvar))

key '12,14' has no corresponding arguments

Obviously the {12,14} is messing up format. Is there a way around this problem and still use the format method for substitution?

Community
  • 1
  • 1

1 Answers1

0

From documentation,

If you need to include a bracing character in the literal text, it can be escaped by doubling:

{{ and }}.

Use

'({})-(\d){{12,14}}.tar.gz'.format(myvar)

Also, format is older way of doing it. Use f-string

f'({myvar})-(\d){{12,14}}.tar.gz'

Why not concatenate directly?

myvar + '-(\d){{12,14}}.tar.gz'
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55