"Zeroing out" the Nth group in a RE

Pekka Niiranen krissepu at vip.fi
Sun Aug 4 15:05:30 EDT 2002


What is needed is version of SRE which re.findall() does not return empty matches!!!

-pekka-

Greg Chapman wrote:

> On Sat, 3 Aug 2002 17:41:18 +0200 (MET DST), Doru-Catalin Togea
> <doru-cat at ifi.uio.no> wrote:
>
> >bibleRef =
> >re.compile(r'(?:(?:(\w+)(?:\s+)(\d+):(\d+))(?:(?:\s*)(?:-)(?:\s*)(\d+))?)')
> >m = bibleRef.findall(refs)
> >print m
> >
> >outputs:
> >
> >[('gen', '5', '17', '23'), ('lev', '14', '20', '23'), ('rev', '19', '10',
> >'25')]
> >
> >which is "mistaken" in that the second tuple should have been
> >
> >       ('lev', '14', '20')             or
> >       ('lev', '14', '20', '')
>
> Which version of Python are you using?  This appears to work correctly using
> 2.2.1:
>
> Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re
> >>> refs = 'gen  5:17 - 23  , lev 14:20, rev 19:10 - 25'
> >>> br = re.compile(r'(?:(?:(\w+)(?:\s+)(\d+):(\d+))(?:(?:\s*)(?:-)(?:\s*)(\d+))?)')
> >>> br.findall(refs)
> [('gen', '5', '17', '23'), ('lev', '14', '20', ''), ('rev', '19', '10', '25')]
>
> ---
> Greg Chapman




More information about the Python-list mailing list