Re: help on python regular expression named group

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

  1. Mohan L

    Mohan L Guest

    On Tue, Jul 16, 2013 at 2:12 PM, Joshua Landau <> wrote:

    > On 16 July 2013 07:55, Mohan L <> wrote:
    > >
    > > 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

    >
    > Why do you need to do this in a single Regex? Can't you just "
    > ".join(..) the date and time?
    >


    I using another third party python script. It takes the regex from
    configuration file. I can't write any code. I have to do all this in single
    regex.



    Thanks
    Mohan L
    Mohan L, Jul 16, 2013
    #1
    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,260
  2. Mohan L
    Replies:
    3
    Views:
    73
  3. Joshua Landau
    Replies:
    0
    Views:
    78
    Joshua Landau
    Jul 16, 2013
  4. MRAB
    Replies:
    0
    Views:
    74
  5. Joshua Landau
    Replies:
    0
    Views:
    74
    Joshua Landau
    Jul 16, 2013
Loading...

Share This Page