python替换部分参数, 进行局部替换
使用\1,\2
import re
s = 'i o u! u 2?'
s = re.sub('([.!?])', ' \\1', s) # re.sub(r'([.!?])', r' \1', s)
print(s)
使用lambda表达式
import re
test_str = 'NumberInt(1),NumberInt(2),NumberInt(3)'
regex = r"(NumberInt\(*)(\d)(\))"
result = re.sub(regex, lambda x:x.group(2), test_str)
网友评论