首页 > 解决方案 > 正则表达式:如何在 python 中使用 re.sub() 将两个完全匹配组合成一个?

问题描述

标签: pythonregexreplace

解决方案


根据您的回答,我相信这是您正在寻找的答案:

import re
text = '“Client” refers to Client or “”any User uploads or otherwise supplies to, or stores in, the Services under Client’s account.'
re.sub(r'[^\w|^\d|^\s]+', '', text)

输出:

'Client refers to Client or any User uploads or otherwise supplies to or stores in the Services under Clients account'

替换所有字符,除了:

  • ^\w单词字符,例如 AZ 和 az
  • ^\d数字
  • ^\s空白

考虑到您的特殊字符列表的广度,这种排他过滤比包容过滤更有效。


推荐阅读