reusing parts of a string in RE matches?

Mirco Wahab peace.is.our.profession at gmx.de
Sat May 13 15:17:41 CEST 2006


Hi Fredrik

you brought up some terse and
somehow expressive lines with
their own beauty ...

> [this] is best done by a list comprehension:
>    l = [m[1] for m in re.findall(r, t)]
> 
> or, [...] a generator expression:
>    g = (m[1] for m in re.findall(r, t))
> 
> or
>    process(m[1] for m in re.findall(r, t))
> 
> ... avoid creating the tuples, ... finditer instead:
>     l = [m.group(2) for m in re.finditer(r, t)]
>     g = (m.group(2) for m in re.finditer(r, t))
> 
> finditer is also a good tool to use 
>     for m in re.finditer(r, t):
>         s = m.group(2)
>         ... process s in some way ...

... which made me wish to internalize such wisdom too ;-)

This looks almost beautiful, it made me stand up and
go to some large book stores in order to grab a good
book on python.

Sadly, there were none (except one small 'dictionary',
ISBN: 3826615123). I live in a fairly large city
in Germany w/three large bookstores in the center,
where one can get loads of PHP and Java books, lots
of C/C++ and the like - even some Ruby books (some
"Rails" too) on display (WTF).

Not that I wouldn't order books (I do that all the
time for 'original versions') but it makes one
sad-faced to see the small impact of the Python
language here today on bookstore-tournarounds ...

Thanks & regards

Mirco



More information about the Python-list mailing list