"Zeroing out" the Nth group in a RE

Greg Chapman glc at well.com
Sun Aug 4 17:55:27 CEST 2002


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