更新时间:2023-02-23 12:54:13
在正则表达式中使用替换函数,提取数字,将它们转换为整数,然后转换为字符:
Use a replacement function in your regex, which extracts the digits, converts them to integer, and then to character:
import re
s = "He#108##108#o"
print(re.sub("#(\d+)#", lambda x : chr(int(x.group(1))), s))
结果:
Hello