Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > help on python regular expression named group

Reply
Thread Tools

help on python regular expression named group

 
 
Mohan L
Guest
Posts: n/a
 
      07-16-2013
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

 
Reply With Quote
 
 
 
 
wxjmfauth@gmail.com
Guest
Posts: n/a
 
      07-17-2013
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

 
Reply With Quote
 
 
 
 
Joshua Landau
Guest
Posts: n/a
 
      07-17-2013
On 17 July 2013 07:15, <(E-Mail Removed)> 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.
 
Reply With Quote
 
wxjmfauth@gmail.com
Guest
Posts: n/a
 
      07-17-2013
Le mercredi 17 juillet 2013 09:46:46 UTC+2, Joshua Landau a écrit*:
> On 17 July 2013 07:15, <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
Named Group support for regular expressions in TR1? DomoChan@gmail.com C++ 3 08-12-2008 01:15 PM
Typed named groups in regular expression Hugo Ferreira Python 5 05-20-2007 10:40 AM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments