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.
No, there is not. You can convert it yourself, but re operates on text, and produces text, that's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With