regex - 从文本中删除大量字符串
问题描述
假设
txt='Daniel Johnson and Ana Hickman are friends. They know each other for a long time. Daniel Johnson is a professor and Ana Hickman is writer.'
是一大段文字,我想删除一大串字符串,例如
removalLists=['Daniel Johnson','Ana Hickman']
从他们。我的意思是我想将列表中的所有元素替换为
' '
我知道我可以使用循环轻松做到这一点,例如
for string in removalLists:
txt=re.sub(string,' ',txt)
我想知道我是否可以更快地做到这一点。
解决方案
一种方法是生成一个单一的正则表达式模式,它是替换术语的交替。所以,我建议使用以下正则表达式模式,例如:
\bDaniel Johnson\b|\bAna Hickman\b
为了生成这个,我们可以首先用单词边界 ( ) 包装每个术语\b
。然后,将列表折叠为单个字符串,|
用作分隔符。最后,我们可以用re.sub
一个空格替换所有出现的任何术语。
txt = 'Daniel Johnson and Ana Hickman are friends. They know each other for a long time. Daniel Johnson is a professor and Ana Hickman is writer.'
removalLists = ['Daniel Johnson','Ana Hickman']
regex = '|'.join([r'\b' + s + r'\b' for s in removalLists])
output = re.sub(regex, " ", txt)
print(output)
and are friends. They know each other for a long time. is a professor and is writer.
推荐阅读
- mysql - MySQL 存储过程没有调用正确的 CASE
- c# - 如何在网格中设置 TextBlock 的边界?
- java - 如何修复'Android资源编译失败输出:values_values.arsc.flat:错误:打开失败。'
- mysql - 按交易顺序组合购买
- javascript - 仅对已编辑的文件启用 eslint
- sql - Oracle 相同的查询在不同的连接上返回不同的结果(取消提交事务)
- python - 尝试在 Python 中检测变量/字符串是否大小写混合
- angular - 使用角度从firebase身份验证中删除用户
- python - 我如何以及在哪里可以在我的本地机器上部署我的 Flask 应用程序构建并连接到我的本地 mysql-server?
- java - 如何从spring cloud stream kafka的ErrorMessage中的payload中获取failedMessage?