fork download
  1. import re
  2. strings = ['PT1M6S', 'PT38S', 'PT58M4']
  3. rx = re.compile(r'^PT(?:(\d+)M)?(?:(\d+)S?)?$')
  4. for s in strings:
  5. print(s, ' => ', rx.sub(lambda x: f"{(x.group(1) or '00').zfill(2)}:{(x.group(2) or '00').zfill(2)}", s))
Success #stdin #stdout 0.02s 9624KB
stdin
Standard input is empty
stdout
PT1M6S  =>  01:06
PT38S  =>  00:38
PT58M4  =>  58:04