使用整数运算的python re.sub

pro*_*ach 1 python regex

我目前有一个执行以下操作的模式替换:

x = re.sub('(\d+)','\g<1>','100')
=> x = 100
Run Code Online (Sandbox Code Playgroud)

我需要能够在替换中将整数除以10,因为模式和替换是来自数据库文本字段的输入(因此我不能使用代码)。有没有办法做到这一点,使=> x = 10

谢谢,理查德

eum*_*iro 6

import re

t = 'a b c 100 200'

f = lambda x: str(int(x.group(0)) / 10)
re.sub('\d+', f, t)

# returns 'a b c 10 20'
Run Code Online (Sandbox Code Playgroud)