how can I check if group member exist ?

Discussion in 'Python' started by Hans, Jun 21, 2013.

  1. Hans

    Hans Guest

    Hi,

    I'm doing a regular expression matching, let's say "a=re.search(re_str,match_str)", if matching, I don't know how many str/item will be extracted from re_str, maybe a.group(1), a.group(2) exist but a.group(3) does not.

    Can I somehow check it? something like:
    if exist(a.group(1)): print a.group(1)
    if exist(a.group(2)): print a.group(2)
    if exist(a.group(3)): print a.group(3)


    I don't want to be hit by "Indexerror":
    >>>print a.group(3)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    IndexError: no such group
    >>>


    thanks!!!
     
    Hans, Jun 21, 2013
    #1
    1. Advertising

  2. Hans

    Peter Otten Guest

    Hans wrote:

    > Hi,
    >
    > I'm doing a regular expression matching, let's say
    > "a=re.search(re_str,match_str)", if matching, I don't know how many
    > str/item will be extracted from re_str, maybe a.group(1), a.group(2) exist
    > but a.group(3) does not.
    >
    > Can I somehow check it? something like:
    > if exist(a.group(1)): print a.group(1)
    > if exist(a.group(2)): print a.group(2)
    > if exist(a.group(3)): print a.group(3)
    >
    >
    > I don't want to be hit by "Indexerror":
    >>>>print a.group(3)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > IndexError: no such group
    >>>>

    >
    > thanks!!!


    You could catch the exception

    for index in itertools.count(1):
    try:
    print a.group(index)
    except IndexError:
    break

    but in this case there's the groups() method:

    for g in a.groups():
    print g


    The interactive interpreter is a good tool to find candidates for a solution
    yourself:

    >>> a = re.compile("(.)(.)(.)").search("alpha")
    >>> dir(a)

    ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__',
    '__format__', '__getattribute__', '__hash__', '__init__', '__new__',
    '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
    '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group',
    'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs',
    'span', 'start', 'string']

    >From the above names lastindex looks promising, too. Can you find out how

    the output of

    for i in range(a.lastindex):
    print a.group(i+1)

    differs from that of looping over groups()?
     
    Peter Otten, Jun 21, 2013
    #2
    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. =?Utf-8?B?VmlubnkgVmlubg==?=

    Check if user is a member of a domain group

    =?Utf-8?B?VmlubnkgVmlubg==?=, Sep 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,998
    Mark Rae
    Sep 27, 2005
  2. Y.S.
    Replies:
    3
    Views:
    1,042
    strajan
    Sep 17, 2003
  3. LT
    Replies:
    7
    Views:
    2,110
    Phlip
    Jul 25, 2004
  4. Iain
    Replies:
    5
    Views:
    1,520
    Alexey Smirnov
    Oct 19, 2009
  5. Markus Mohr
    Replies:
    7
    Views:
    261
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page