列表是a = ['Aeroplane','Ramanujan','World-king','Pizza/Burger']
我试图用Pizza_Burger
和World_king
替换列表中的项目(用-
、/
)。无论符号是什么,都应替换为和下划线。你知道吗
这是我的密码:
import re
def replaceStrings(arg):
txt =arg
res = re.search(r'(?i)\b([a-z][a-z0-9_]*)([/-]+)([a-z][a-z0-9_]*)\b', txt)
if res:
pp = reg.sub(r'\1_\2',txt)
print(pp)
return pp
for i in a:
replaceStrings(i)
但是我没有得到想要的结果。我的正则表达式有什么问题。 我是正则表达式的初学者。谢谢
清理术语的一个简单方法是循环遍历术语并分别清理每个术语。你可以做一些简单的事情,比如
'World-king'.replace('/','_').replace('-','_')
或者可以使用regex进行如下清理:
输出:
更新:[评论由OP添加]
如果要在清洁前确保图案匹配,也可以执行以下操作:
输出:
相关问题 更多 >
编程相关推荐