Typed named groups in regular expression

Paddy paddy3118 at googlemail.com
Sat May 19 01:32:43 EDT 2007


On May 16, 6:58 pm, "Hugo Ferreira" <byt... at gmail.com> wrote:
> Hi!
>
> Is it possible to "automagically" coerce the named groups to python types? e.g.:
>
> >>> type(re.match('(?P<x>\d*)', '123').groupdict()['x'])
>
> <type 'str'>
>
> But what I'm looking forward is for the type to be 'int'.
>
> Cheers!
>
> Hugo Ferreira

If you do a ot of that sort of thing in many programs
then it might be worth your while to set up a framework
that does it. Something like adding an underscore
then the name of a type conversion function to all
group names, and creating a function to apply the
type convertion function to all named groups of a
match object.
- Paddy.




More information about the Python-list mailing list