Iterate over group names in a regex match?

Discussion in 'Python' started by Brian D, Jan 19, 2010.

  1. Brian D

    Brian D Guest

    Here's a simple named group matching pattern:

    >>> s = "1,2,3"
    >>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    >>> m = re.match(p, s)
    >>> m

    <_sre.SRE_Match object at 0x011BE610>
    >>> print m.groups()

    ('1', '2', '3')

    Is it possible to call the group names, so that I can iterate over
    them?

    The result I'm looking for would be:

    ('one', 'two', 'three')
    Brian D, Jan 19, 2010
    #1
    1. Advertising

  2. Brian D

    Peter Otten Guest

    Brian D wrote:

    > Here's a simple named group matching pattern:
    >
    >>>> s = "1,2,3"
    >>>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    >>>> m = re.match(p, s)
    >>>> m

    > <_sre.SRE_Match object at 0x011BE610>
    >>>> print m.groups()

    > ('1', '2', '3')
    >
    > Is it possible to call the group names, so that I can iterate over
    > them?
    >
    > The result I'm looking for would be:
    >
    > ('one', 'two', 'three')


    >>> s = "1,2,3"
    >>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    >>> m = re.match(p, s)
    >>> dir(m)

    ['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
    'groups', 'span', 'start']
    >>> m.groupdict().keys()

    ['one', 'three', 'two']
    >>> sorted(m.groupdict(), key=m.span)

    ['one', 'two', 'three']

    Peter
    Peter Otten, Jan 19, 2010
    #2
    1. Advertising

  3. * Brian D:
    > Here's a simple named group matching pattern:
    >
    >>>> s = "1,2,3"
    >>>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    >>>> m = re.match(p, s)
    >>>> m

    > <_sre.SRE_Match object at 0x011BE610>
    >>>> print m.groups()

    > ('1', '2', '3')
    >
    > Is it possible to call the group names, so that I can iterate over
    > them?
    >
    > The result I'm looking for would be:
    >
    > ('one', 'two', 'three')


    I never used that beast (I'm in a sense pretty new to Python, although starting
    some months back I only investigate what's needed for my writings), but checking
    things in the interpreter:


    >>> import re
    >>> re

    <module 're' from 'C:\Program Files\cpython\python26\lib\re.pyc'>
    >>> s = "1,2,3"
    >>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    >>> m = re.match(p, s)
    >>> m

    <_sre.SRE_Match object at 0x01319F70>
    >>> m.groups()

    ('1', '2', '3')
    >>> type( m.groups() )

    <type 'tuple'>
    >>> dir( m )

    ['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups',
    'span', 'start']
    >>> m.groupdict

    <built-in method groupdict of _sre.SRE_Match object at 0x01319F70>
    >>> m.groupdict()

    {'one': '1', 'three': '3', 'two': '2'}
    >>> print( tuple( m.groupdict().keys() ) )

    ('one', 'three', 'two')
    >>> _



    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Jan 19, 2010
    #3
  4. Brian D

    Brian D Guest

    On Jan 19, 11:28 am, Peter Otten <> wrote:
    > Brian D wrote:
    > > Here's a simple named group matching pattern:

    >
    > >>>> s = "1,2,3"
    > >>>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    > >>>> m = re.match(p, s)
    > >>>> m

    > > <_sre.SRE_Match object at 0x011BE610>
    > >>>> print m.groups()

    > > ('1', '2', '3')

    >
    > > Is it possible to call the group names, so that I can iterate over
    > > them?

    >
    > > The result I'm looking for would be:

    >
    > > ('one', 'two', 'three')
    > >>> s = "1,2,3"
    > >>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    > >>> m = re.match(p, s)
    > >>> dir(m)

    >
    > ['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
    > 'groups', 'span', 'start']>>> m.groupdict().keys()
    >
    > ['one', 'three', 'two']>>> sorted(m.groupdict(), key=m.span)
    >
    > ['one', 'two', 'three']
    >
    > Peter


    groupdict() does it. I've never seen it used before. Very cool!

    Thank you all for taking time to answer the question.
    Brian D, Jan 19, 2010
    #4
  5. Brian D

    Brian D Guest

    On Jan 19, 11:51 am, Brian D <> wrote:
    > On Jan 19, 11:28 am, Peter Otten <> wrote:
    >
    >
    >
    > > Brian D wrote:
    > > > Here's a simple named group matching pattern:

    >
    > > >>>> s = "1,2,3"
    > > >>>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    > > >>>> m = re.match(p, s)
    > > >>>> m
    > > > <_sre.SRE_Match object at 0x011BE610>
    > > >>>> print m.groups()
    > > > ('1', '2', '3')

    >
    > > > Is it possible to call the group names, so that I can iterate over
    > > > them?

    >
    > > > The result I'm looking for would be:

    >
    > > > ('one', 'two', 'three')
    > > >>> s = "1,2,3"
    > > >>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    > > >>> m = re.match(p, s)
    > > >>> dir(m)

    >
    > > ['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
    > > 'groups', 'span', 'start']>>> m.groupdict().keys()

    >
    > > ['one', 'three', 'two']>>> sorted(m.groupdict(), key=m.span)

    >
    > > ['one', 'two', 'three']

    >
    > > Peter

    >
    > groupdict() does it. I've never seen it used before. Very cool!
    >
    > Thank you all for taking time to answer the question.



    FYI, here's an example of the working result ...

    >>> for k, v in m.groupdict().iteritems():

    k, v


    ('one', '1')
    ('three', '3')
    ('two', '2')


    The use for this is that I'm pulling data from a flat text file using
    regex, and storing values in a dictionary that will be used to update
    a database.
    Brian D, Jan 19, 2010
    #5
  6. Brian D

    MRAB Guest

    Brian D wrote:
    > Here's a simple named group matching pattern:
    >
    >>>> s = "1,2,3"
    >>>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
    >>>> m = re.match(p, s)
    >>>> m

    > <_sre.SRE_Match object at 0x011BE610>
    >>>> print m.groups()

    > ('1', '2', '3')
    >
    > Is it possible to call the group names, so that I can iterate over
    > them?
    >
    > The result I'm looking for would be:
    >
    > ('one', 'two', 'three')
    >

    The closest you can get is with groupdict():

    >>> print m.groupdict()

    {'one': '1', 'three': '3', 'two': '2'}
    MRAB, Jan 19, 2010
    #6
    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. Gogo
    Replies:
    1
    Views:
    2,076
    Sudsy
    Sep 4, 2003
  2. hiwa
    Replies:
    0
    Views:
    624
  3. runescience
    Replies:
    0
    Views:
    1,428
    runescience
    Feb 9, 2006
  4. John
    Replies:
    4
    Views:
    890
    RedGrittyBrick
    Apr 1, 2008
  5. RolfK
    Replies:
    1
    Views:
    1,830
    Martin Honnen
    Jun 7, 2009
Loading...

Share This Page