【问题标题】:Delete all punctuation symbols exept underscore and curly braces in string in python?删除python中字符串中除下划线和大括号之外的所有标点符号?
【发布时间】:2019-05-16 05:14:37
【问题描述】:

假设我有一个字符串

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

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

【问题讨论】:

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


【解决方案1】:

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

import re

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

输出为Hey {customer_name} whats up

【讨论】:

  • 你是对的,修正!谢谢@AhmedAbdelhameed :) 请检查它是否看起来不错,如果是,请考虑投票:)
猜你喜欢
  • 2017-03-26
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多