我有以下字符串。
'___5weeks_rollingAverage_8hours'
'___5weeks__rolling=*%Average_8hours'
'___5weeks_rollingAverage_8hours__'
'___5weeks_rollingAverage_8hours'
我想删除 _
如果它在字符串中的任何地方出现一次以上。所以上面的内容应该翻译成
5weeks_rollingAverage_8hours
tvm
你可以使用你说的正则表达式,对于非字母数字,我们可以使用[^a-zA-Z0-9],所以代码是这样的。
import re
regex = re.compile(r'[^a-zA-Z0-9]{2,}')
t = '___5weeks_rollingAverage___8h**ou&%rs'
result = regex.sub("",t)
print(result)
输出: 5周_滚动平均8小时
用regex
import re
test_str = ("___5weeks_rollingAverage_8hours", "___5weeks__rolling=*%Average_8hours",
"___5weeks_rollingAverage_8hours__", "___5weeks_rollingAverage_8hours")
regex = re.compile(r"[!\"#$%&'()*+,-.\/:;<=>?@\[\\\]^_`{|}~]{2,}")
for item in test_str:
item = regex.sub('', item)
print(item)
产出
5weeks_rollingAverage_8hours
5weeksrollingAverage_8hours
5weeks_rollingAverage_8hours
5weeks_rollingAverage_8hours