删除python中所有下划线和大括号的标点符号?

时间:2019-05-16 05:14:37

标签: regex python-3.x regex-negation

假设我有一个字符串

s = "Hey, {customer_name}, what's up?"

除去下划线和花括号以外的所有标点符号的合适正则表达式是什么?

1 个答案:

答案 0 :(得分:1)

您可以将re.sub与模式[^\w _{}]一起使用,该模式将忽略所有字母数字字符,但将包括下划线_和花括号{}

import re

s = "Hey, {customer_name}, what's up?"
print(re.sub(r'[^\w {}]','',s))

输出为Hey {customer_name} whats up