Some more odd behaviour from the Regexp library

Doug Schwarz see at sig.for.address.edu
Wed Oct 19 23:31:51 EDT 2005


In article <1129777843.892280.302490 at g44g2000cwa.googlegroups.com>,
 "David Veerasingam" <vdavidster at gmail.com> wrote:

> Can anyone explain why it won't give me my captured group?
> 
> In [1]: a = 'exit: gkdfjgfjdfsgdjglkghdfgkd'
> In [2]: import re
> In [3]: b = re.search(r'exit: (.*?)', a)
> In [4]: b.group(0)
> Out[4]: 'exit: '
> 
> In [5]: b.group(1)
> Out[5]: ''
> 
> In [6]: b.group(2)
> IndexError: no such group

The ? tells (.*?) to match as little as possible and that is nothing.  
If you change it to (.*) it should do what you want.

-- 
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.



More information about the Python-list mailing list