python - 删除python中字符串中除下划线和大括号之外的所有标点符号?
问题描述
假设我有一个字符串
s = "Hey, {customer_name}, what's up?"
删除除下划线和花括号外的所有标点符号的适当正则表达式是什么?
解决方案
您可以使用re.sub
with 模式[^\w _{}]
,它会忽略所有字母数字字符,但会包括下划线_
和花括号{}
import re
s = "Hey, {customer_name}, what's up?"
print(re.sub(r'[^\w {}]','',s))
输出是Hey {customer_name} whats up
推荐阅读
- css - 如果 scss 中的输入为空,如何禁用或忽略代码
- c++ - 每次打开组合框时如何创建项目列表?
- c++ - 我如何找到长度?我有 8 个管子和 11 个节点
- macos - 解决警告 NETSDK1138:目标框架“netcoreapp3.0”不受支持
- node.js - “node_modules\uuidv4\build\lib\uuidv4.js”的包试图导入Node标准库模块“util”
- python - 循环字典列表并比较其他列表中的任何匹配值
- asp.net-mvc - 在 Asp.net Mvc 5 中使用 Active Directory 登录?
- vim - 维姆。无法将非英语字符拉到系统剪贴板
- regex - 正则表达式提取包含 PIPE (|) 的子字符串
- python - check if key is present in the row as a substring