Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I specify regex group to return float or int instead of string?

0 views
Skip to first unread message

Jeremy

unread,
Feb 25, 2010, 10:48:44 AM2/25/10
to
I have a regular expression that searches for some numbers and puts
them into a dictionary, i.e.

'(?P<integer>\d+)\s+(?P<float>\d+\.\d+)'

Is it possible to have the results of the matches returned as int or
float objects instead of strings?

Thanks,
Jeremy

Steven D'Aprano

unread,
Feb 25, 2010, 11:41:01 AM2/25/10
to

No. Just convert the match with int() or float() before storing it in the
dictionary. That is, instead of:

d[key] = match

use

d[key] = float(match)

or similar.

--
Steven

Jeremy

unread,
Feb 25, 2010, 12:00:07 PM2/25/10
to
On Feb 25, 9:41 am, Steven D'Aprano <st...@REMOVE-THIS-

I was afraid that was the only option. Oh well, thought I'd ask
anyway. Thanks for your help.
Jeremy

Steven D'Aprano

unread,
Feb 25, 2010, 12:20:44 PM2/25/10
to


Why "afraid"? What's the problem with calling int or float on the match?


--
Steven

nn

unread,
Feb 25, 2010, 3:09:12 PM2/25/10
to
On Feb 25, 12:20 pm, Steven D'Aprano <st...@REMOVE-THIS-

He must not be Dutch

:-) SCNR

0 new messages