By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Typed named groups in regular expression

P: n/a
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
May 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hello Hugo,
Is it possible to "automagically" coerce the named groups to python types? e.g.:
Not that I know of, however I use the following idiom:

match = my_regexp.find(some_string)
def t(name, convert=str):
return convert(match.group(name))

myint = t("field1", int)

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com

May 17 '07 #2

P: n/a
On May 16, 6:58 pm, "Hugo Ferreira" <byt...@gmail.comwrote:
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.

May 19 '07 #3

P: n/a
On May 19, 12:32 am, Paddy <paddy3...@googlemail.comwrote:
On May 16, 6:58 pm, "Hugo Ferreira" <byt...@gmail.comwrote:
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

May 19 '07 #4

P: n/a
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 <pt***@austin.rr.comwrote:
On May 19, 12:32 am, Paddy <paddy3...@googlemail.comwrote:
On May 16, 6:58 pm, "Hugo Ferreira" <byt...@gmail.comwrote:
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
May 20 '07 #5

P: n/a
On May 20, 2:27 am, "Hugo Ferreira" <byt...@gmail.comwrote:
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...
Hackis, hackISH!
Sir, I would have you know that the idea proffered is a hack in the
finest traditions of hackerdom.

Why, the solution is guaranteed to work for the longest time with the
smallest expenditure and be delivered in the quickest time and be good
enough - but no more.

So, promote the idea to a hack or its pistols at dawn!
:-)

- Paddy.

May 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.