Jon Crump wrote: > Dear all, > > I've been around and around with this and can't seem to conceptualize it > properly. > > I've got a javascript object in a text file that I'd like to treat as > json so that I can import it into a python program via > simplejson.loads(); however, it's not proper json because it has new > Date() object declarations in it. So I thought to pre-process it by > translating the dates into ISO format, but RE is making me cross-eyed. > > example string: > > s = """{"title" : "Hebertot, Normandie", "start" : new Date(1203,10,7), > "description" : "Hardy's long name: Hebertot, Normandie. <br> > <img src=\"document.png\" style=\"cursor: pointer\" > onclick=\"SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); > return false\"/>pg.035: 1203-10-10 to 1203-11-18"},{"title" : > "Newark-Upon-Trent, Nottinghamshire", "start" : new Date(1216,9,16), > "end" : new Date(1216,9,18), "description" : "Hardy's long name: > Newark-Upon-Trent, Nottinghamshire. <br> "}""" > > I can locate the dates with: > jdate = re.compile('new Date\(\d{4},\d{1,2},\d{1,2}\)')
I think you're pretty close... # note the extra parens jdate = re.compile('new Date\((\d{4}),(\d{1,2}),(\d{1,2})\)') ... then ... print jdate.sub(r'"\1-\2-\3"', s) {"title" : "Hebertot, Normandie", "start" : "1203-10-7", "description" : "Hardy's long name: Hebertot, Normandie. <br> <img src="document.png" style="cursor: pointer" onclick="SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false"/>pg.035: 1203-10-10 to 1203-11-18"},{"title" : "Newark-Upon-Trent, Nottinghamshire", "start" : "1216-9-16", "end" : "1216-9-18", "description" : "Hardy's long name: Newark-Upon-Trent, Nottinghamshire. <br> "} You can also use named groups which *might* help make clearer what's happening above, something like: jdate = re.compile('new Date\((?P<Year>\d{4}),(?P<Month>\d{1,2}),(?P<Day>\d{1,2})\)') print jdate.sub(r'"\g<Year>-\g<Month>-\g<Day>"', s) More info here: http://docs.python.org/library/re.html#re.sub HTH, Marty _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor