我希望完成以下内容,并且想知道是否有人建议如何最好地去做.
我有一个字符串,说'this-is,-toronto.-and-this-is,-boston',我想将所有出现的', - [az]'转换为', - [AZ]'.在这种情况下,转换的结果将是'this-is,-Toronto.-and-this-is,-Boston'.
我一直在尝试使用re.sub(),但尚未弄清楚如何使用
testString = 'this-is,-toronto.-and-this-is,-boston'
re.sub(r',_([a-z])', r',_??', testString)
Run Code Online (Sandbox Code Playgroud)
谢谢!
Ned*_*der 11
re.sub可以使用一个返回替换字符串的函数:
import re
s = 'this-is,-toronto.-and-this-is,-boston'
t = re.sub(',-[a-z]', lambda x: x.group(0).upper(), s)
print t
Run Code Online (Sandbox Code Playgroud)
版画
this-is,-Toronto.-and-this-is,-Boston
Run Code Online (Sandbox Code Playgroud)