删除python中字符串中除下划线和花括号外的所有标点符号?

Delete all punctuation symbols exept underscore and curly braces in string in python?

假设我有一个字符串

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

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

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

import re

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

输出为Hey {customer_name} whats up