help on python regular expression named group

Discussion in 'Python' started by Mohan L, Jul 16, 2013.

  1. Mohan L

    Mohan L Guest

    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
    Mohan L, Jul 16, 2013
    #1
    1. Advertising

  2. Mohan L

    Guest

    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
    , Jul 17, 2013
    #2
    1. Advertising

  3. On 17 July 2013 07:15, <> 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.
    Joshua Landau, Jul 17, 2013
    #3
  4. Mohan L

    Guest

    Le mercredi 17 juillet 2013 09:46:46 UTC+2, Joshua Landau a écrit :
    > On 17 July 2013 07:15, <> 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
    , Jul 17, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,272
  2. Joshua Landau
    Replies:
    0
    Views:
    80
    Joshua Landau
    Jul 16, 2013
  3. Mohan L
    Replies:
    0
    Views:
    76
    Mohan L
    Jul 16, 2013
  4. MRAB
    Replies:
    0
    Views:
    76
  5. Joshua Landau
    Replies:
    0
    Views:
    75
    Joshua Landau
    Jul 16, 2013
Loading...

Share This Page