且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python正则表达式替换为ASCII值

更新时间: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