how to custom format on print from a regex match using python -


using python, need extract id , date filename:

export-foobar-54321-2015_02_18_23_30_00.csv.gz

where: id = 54321 date = 2015_02_18

so far, can match file name regular expression:

export-foobar-[0-9]{5}\-[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}.csv.gz

what i'd final print out put be:

id = 54321 date =02-18-2015

being new python, tried following, however, im not sure how print need. have far:

>>> import re >>> filename='export-generic-33605-2015_02_18_23_30_00.csv.gz' >>> matches=re.search("export-foobar-[0-9]{5}\-[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}.csv.gz",filename) >>> print(matches) <_sre.sre_match object @ 0x7f2ee3616718> 

if please in printing out need , customizing print match date mm-dd-yyyy correctly appreciated.

you can use following capture digits of interest , rearrange date. generic in regexp changed \w+ capture text string.

filename = 'export-foobar-54321-2015_02_18_23_30_00.csv.gz'  matches=re.search(r"export-\w+-([0-9]{5})-([0-9]{4})_([0-9]{2})_([0-9]{2})_[0-9]{2}_[0-9]{2}_[0-9]{2}\.csv\.gz",filename).groups() id, year, month, day = matches date = '-'.join([month, day, year])  print(id) # 54321 print(date) # 02-18-2015 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -