Typed named groups in regular expression

Hugo Ferreira bytter at gmail.com
Sat May 19 21:27:47 EDT 2007


Both Paddy (hackish) and McGuire (right tool for the job) ideas sound
very interesting ;-) I'll definitely research on them further.

Thanks for the support...

On 19 May 2007 04:39:58 -0700, Paul McGuire <ptmcg at austin.rr.com> wrote:
> On May 19, 12:32 am, Paddy <paddy3... at googlemail.com> wrote:
> > 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.
>
> pyparsing might just be this sort of framework, in that you can attach
> parse actions to elements within a grammar.  At parse time, the parse
> action is called with the list of tokens currently matched.
>
> >>> from pyparsing import Regex
> >>> re = Regex( r"(\d*)" ).setResultsName("x")\
> ...        .setParseAction(lambda t:int(t[0]))
> >>> results = re.parseString("123")
> >>> print results.x
> 123
>
> -- Paul
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list