Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50731 > unrolled thread

help on python regular expression named group

Started byMohan L <l.mohanphy@gmail.com>
First post2013-07-16 12:25 +0530
Last post2013-07-17 01:00 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  help on python regular expression named group Mohan L <l.mohanphy@gmail.com> - 2013-07-16 12:25 +0530
    Re: help on python regular expression named group wxjmfauth@gmail.com - 2013-07-16 23:15 -0700
      Re: help on python regular expression named group Joshua Landau <joshua@landau.ws> - 2013-07-17 08:46 +0100
        Re: help on python regular expression named group wxjmfauth@gmail.com - 2013-07-17 01:00 -0700

#50731 — help on python regular expression named group

FromMohan L <l.mohanphy@gmail.com>
Date2013-07-16 12:25 +0530
Subjecthelp on python regular expression named group
Message-ID<mailman.4760.1373960601.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [next] | [standalone]


#50776

Fromwxjmfauth@gmail.com
Date2013-07-16 23:15 -0700
Message-ID<375b2687-272d-4139-a500-823fe0a9f774@googlegroups.com>
In reply to#50731
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

[toc] | [prev] | [next] | [standalone]


#50779

FromJoshua Landau <joshua@landau.ws>
Date2013-07-17 08:46 +0100
Message-ID<mailman.4796.1374047254.3114.python-list@python.org>
In reply to#50776
On 17 July 2013 07:15,  <wxjmfauth@gmail.com> wrote:
> Not sure, I'm correct. I took you precise string to
> refresh my memory.

I'm glad to see you doing something else, but I don't think you
understood his problem. Note that his problem has not solution, which
a few seconds of Googling has confirmed to me.

[toc] | [prev] | [next] | [standalone]


#50780

Fromwxjmfauth@gmail.com
Date2013-07-17 01:00 -0700
Message-ID<906b0b97-478e-472f-9987-7e5a8d074397@googlegroups.com>
In reply to#50779
Le mercredi 17 juillet 2013 09:46:46 UTC+2, Joshua Landau a écrit :
> On 17 July 2013 07:15,  <wxjmfauth@gmail.com> wrote:
> 
> > Not sure, I'm correct. I took you precise string to
> 
> > refresh my memory.
> 
> 
> 
> I'm glad to see you doing something else, but I don't think you
> 
> understood his problem. Note that his problem has not solution, which
> 
> a few seconds of Googling has confirmed to me.

Right.

I did not pay attention to "date", "time" *and* "datetime".

jmf

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web