regex - python re.sub with re.search not working as expected -


i'm trying generate output file name , path using timestamp. parts works expected, re.sub not giving me result want. i'm doing wrong here?

>>> = r'\output\end-of-day\{%y\%m\%d\uinvdeff securities %m.%d.%y}.xlsx' >>> b = re.search(r'(?p<timestamp>\{[^}]*(%[a-za-z])[^{]*\})', a) >>> b.group('timestamp') '{%y\\%m\\%d\\uinvdeff securities %m.%d.%y}'  >>> datetime.datetime(2014, 8, 18, 11, 43, 47, 55000)  >>> now.strftime(b.group('timestamp'))[1:-1] '2014\\08\\18\\uinvdeff securities 08.18.2014'  >>> re.sub(b.group('timestamp'), now.strftime(b.group('timestamp'))[1:-1], a) '\\output\\end-of-day\\{%y\\%m\\%d\\uinvdeff securities %m.%d.%y}.xlsx' 

expecting '\output\end-of-day\2014\08\18\uinvdeff securities 08.18.2014.xlsx'

the re.sub call interpreting first argument regex pattern:

{%y\\%m\\%d\\uinvdeff securities %m.%d.%y} 

when want replace substring in string use string.replace:

a.replace(b.group('timestamp'), now.strftime(b.group('timestamp'))[1:-1]) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -