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


Groups > comp.lang.python > #50746

Re: help on python regular expression named group

Date 2013-07-16 16:38 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: help on python regular expression named group
References <CADiHtmufOLNysgOP7Qoh70_aT+r3rhKFS=9qgd_AcDNgEFVnqw@mail.gmail.com> <CAN1F8qUpxVuNV-9+9NT1zCzkXQKMMwMtTdCv0+TJ-P9wnigcjg@mail.gmail.com> <CADiHtmum9=otNTuFCwwzsmyKSq_4126D2AnpdWoXGBhEABZHow@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4774.1373989068.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 16/07/2013 11:18, Mohan L wrote:
>
>
>
> On Tue, Jul 16, 2013 at 2:12 PM, Joshua Landau <joshua@landau.ws
> <mailto:joshua@landau.ws>> wrote:
>
>     On 16 July 2013 07:55, Mohan L <l.mohanphy@gmail.com
>     <mailto:l.mohanphy@gmail.com>> 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.
>
A capture group captures a single substring.

What you're asking is for it to with capture 2 substrings (the date and
the time) and then join them together, or capture 1 substring and then
remove part of it.

I don't know of _any_ regex implementation that lets you do that.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: help on python regular expression named group MRAB <python@mrabarnett.plus.com> - 2013-07-16 16:38 +0100

csiph-web