Le mardi 16 juillet 2013 08:55:58 UTC+2, Mohan L a écrit : > Dear All, > > > > Here is my script : > > > > #!/usr/bin/python > > > import re > > > > > # A string. > logs = "date=2012-11-28 time=21:14:59" > > > > # Match with named groups. > m = > re.match("(?P<datetime>(date=(?P<date>[^\s]+))\s+(time=(?P<time>[^\s]+)))", > logs) > > > > # print > > > print m.groupdict() > > > Output: > > ======== > > > {'date': '2012-11-28', 'datetime': 'date=2012-11-28 time=21:14:59', 'time': > '21:14:59'} > > > > > > Required output : > > ================== > > > {'date': '2012-11-28', 'datetime': '2012-11-28 21:14:59', 'time': '21:14:59'} > > > > need help to correct the below regex > > > > > > (?P<datetime>(date=(?P<date>[^\s]+))\s+(time=(?P<time>[^\s]+)))" > > > > > so that It will have : 'datetime': '2012-11-28 21:14:59' instead of > 'datetime': 'date=2012-11-28 time=21:14:59' > > > > > any help would be greatly appreciated > > > > Thanks > Mohan L
------ Not sure, I'm correct. I took you precise string to refresh my memory. >>> import re >>> tmp = 'date=\d{4}-\d{2}-\d{2}' >>> DatePattern = '(?P<DATEPATTERN>' + tmp + ')' >>> tmp = 'time=\d{2}:\d{2}:\d{2}' >>> TimePattern = '(?P<TIMEPATTERN>' + tmp + ')' >>> pattern = DatePattern + ' ' + TimePattern >>> pattern '(?P<DATEPATTERN>date=\\d{4}-\\d{2}-\\d{2}) (?P<TIMEPATTERN>time=\\d{2}:\\d{2}:\\d{2})' >>> CompiledPattern = re.compile(pattern) >>> s = 'date=2012-11-28 time=21:14:59' >>> mo = CompiledPattern.search(s) >>> print(mo) <_sre.SRE_Match object at 0x02CD4188> >>> print(mo.groups()) ('date=2012-11-28', 'time=21:14:59') >>> print(mo.groupdict()) {'DATEPATTERN': 'date=2012-11-28', 'TIMEPATTERN': 'time=21:14:59'} >>> print(mo.group(1), mo.group('DATEPATTERN')) date=2012-11-28 date=2012-11-28 >>> print(mo.group(2), mo.group('TIMEPATTERN')) time=21:14:59 time=21:14:59 >>> jmf -- http://mail.python.org/mailman/listinfo/python-list