While replacing a pattern in a string,
I specifically need the integer/long value of matching named group.
Example of case and what I tried:
status = {1:'foo', 23:'bar'}
re.sub(
'<status>(?P<id>\d+)',
status.get(int(r'\g<id>')), # ValueError: invalid literal for int() with base 10: '\\g<id>'
# status.get(int(r'\g<id>'.decode())), # ValueError: invalid literal for int() with base 10: '\\g<id>'
# status.get('%d' % r'\g<id>'), # %d format: a number is required, not str
'Tom ran: from <status>1 to <status>23')
Normal casting works well with raw string int(r'22') but it doesn’t work in above?
This should work for you: