0
我想根据字典中的值替换我的字符串。我想用正则表达式来试试这个。对字符进行分组并执行替换
d = { 't':'ch' , 'r' : 'gh'}
s = ' Text to replace '
m = re.search('#a pattern to just get each character ',s)
m.group() # this should get me 'T' 'e' 'x' 't' .....
# how can I replace each character in string S with its corresponding key: value in my dictionary? I looked at re.sub() but could figure out how it can be used here.
我要生成的输出 - > Texch町gheplace
该程序的作品! ,我想在这里理解第4行,请问你能告诉我什么 - > pattern ='|'.join(地图(re.escape,d))能帮助你吗? – NBA
@NBA,正则表达式't | r'匹配't'或'r'。 – falsetru
@NBA,这个表达式基本上类似于'''.join(['t','r'])',这产生了't | r'。 – falsetru