doing hundreds of re.subs efficiently on large strings

Anders J. Munch andersjm at dancontrol.dk
Thu Mar 27 03:36:34 EST 2003


"nihilo" <exnihilo at NOmyrealCAPSbox.com> wrote:
> How do I use finditer to find the unmatched parts also (the stuff 
> between the matches)? It looks to me like it just gives you access to 
> the stuff that is matched, but maybe I'm missing something.

Look at start() and end().

>>> s = 'hi nihilo'
>>> for m in re.finditer('(n)|(o)', s):
...     print m.start(), m.end()
... 
3 4
8 9
>>> s[:3], s[3:4], s[4:8], s[8:9], s[9:]
('hi ', 'n', 'ihil', 'o', '')
>>> 

- Anders







More information about the Python-list mailing list