reusing parts of a string in RE matches?
Mirco Wahab
peace.is.our.profession at gmx.de
Thu May 11 10:41:16 EDT 2006
Hi John
> rg = r'(\w)(?=(.)\1)'
>
> That would at least isolate the number, although you'd still have to get
> it out of the list/tuple.
I have no idea how to do this
in Python in a terse way - but
I'll try ;-)
In Perl, its easy. Here, the
"match construct" (\w)(?=(.)\1)
returns all captures in a
list (a 1 a 2 a 4 b 7 c 9)
because we capture 2 fields per
comparision:
The first is (\w), needed for
backreference, the second is
the dot (.), which finds the
number in the center (or any-
thing else).
So, in perl you 'filter' by the
'grep' function on each list
element: grep{ /\d/ } - this
means, only numbers (\d) will
pass through:
$_ = 'a1a2a3Aa4a35a6b7b8c9c';
print grep{/\d/} /(\w)(?=(.)\1)/g;
#prints => 1 2 4 7 9
I'll try to fiddle somthing out
that works in Python too ...
Regards
M.
More information about the Python-list
mailing list