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


Groups > comp.lang.python > #48854

Re: how can I check if group member exist ?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'output': 0.05; 'subject:group': 0.05; 'extracted': 0.09; 'method:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; "'__doc__',": 0.16; 'differs': 0.16; 'indexerror:': 0.16; 'looping': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:member': 0.16; 'thanks!!!': 0.16; 'exception': 0.16; 'index': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'candidates': 0.23; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'skip:" 30': 0.26; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; "skip:' 10": 0.31; 'too.': 0.31; '"",': 0.31; '>>>>': 0.31; 'file': 0.32; 'regular': 0.32; '(most': 0.33; 'not.': 0.33; 'maybe': 0.34; 'could': 0.34; 'except': 0.35; 'tool': 0.35; 'something': 0.35; 'but': 0.35; 'doing': 0.36; 'hi,': 0.36; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'catch': 0.60; 'expression': 0.60; 'break': 0.61; 'such': 0.63; 'skip:r 30': 0.69; 'subject:check': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: how can I check if group member exist ?
Date Fri, 21 Jun 2013 11:25:02 +0200
Organization None
References <caa3e87c-922e-49e2-ae16-7502b1b365d3@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p508493cd.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3658.1371806649.3114.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371806649 news.xs4all.nl 15956 [2001:888:2000:d::a6]:54763
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:48854

Show key headers only | View raw


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()?

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


Thread

how can I check if group member exist ? Hans <hansyin@gmail.com> - 2013-06-21 02:07 -0700
  Re: how can I check if group member exist ? Peter Otten <__peter__@web.de> - 2013-06-21 11:25 +0200
  Re: how can I check if group member exist ? Ben Finney <ben+python@benfinney.id.au> - 2013-06-21 19:49 +1000

csiph-web