Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define or determine type integer in python regex

Is it possible in Python to have the type of a capture group be an integer?
Let's assume I have the following regex:

>>> import re
>>> p = re.compile('[0-9]+')
>>> re.search(p, 'abc123def').group(0)
'123'

I wish that the type of '123' in the group was int, since it can only match integers. It feels like there has to be a better way than defining to only match numbers and then having to convert it to an int afterwards nevertheless.
The background is that I have a complex regex with multiple named capture groups, and some of those capture groups only match integers. I would like those capture groups to be of type integer.

like image 350
Tim Zimmermann Avatar asked Jul 14 '26 09:07

Tim Zimmermann


1 Answers

No, there is not. You can convert it yourself, but re operates on text, and produces text, that's it.

like image 63
ShadowRanger Avatar answered Jul 15 '26 22:07

ShadowRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!