Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48849 > unrolled thread
| Started by | Hans <hansyin@gmail.com> |
|---|---|
| First post | 2013-06-21 02:07 -0700 |
| Last post | 2013-06-21 19:49 +1000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Hans <hansyin@gmail.com> |
|---|---|
| Date | 2013-06-21 02:07 -0700 |
| Subject | how can I check if group member exist ? |
| Message-ID | <caa3e87c-922e-49e2-ae16-7502b1b365d3@googlegroups.com> |
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!!!
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-06-21 11:25 +0200 |
| Message-ID | <mailman.3658.1371806649.3114.python-list@python.org> |
| In reply to | #48849 |
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()?
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2013-06-21 19:49 +1000 |
| Message-ID | <mailman.3659.1371808186.3114.python-list@python.org> |
| In reply to | #48849 |
Hans <hansyin@gmail.com> writes:
> 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
Can you show an example of the pattern, and several examples of text you
want to match? This will help to understand your issue.
Also, your terminology is using some confusing names. Try:
match = re.search(pattern, text)
That will use names which make it easier to think about the code.
--
\ “Ours is a world where people don't know what they want and are |
`\ willing to go through hell to get it.” —Donald Robert Perry |
_o__) Marquis |
Ben Finney
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web