python - 组合三个正则表达式

标签 python regex

有没有办法将以下三个表达式组合成一个正则表达式?

name = re.sub(r'\s?\(\w+\)', '',name) # John Smith (ii) --> John Smith
name = re.sub(r'\s?(Jr.|Sr.)$','', name, flags=re.I) # John Jr. --> John
name = re.sub(r'".+"\s?', '', name) # Dwayne "The Rock" Johnson --> Dwayne Johnson

最佳答案

您可以只使用分组和管道:

re.sub(r'(\s?\(\w+\))|(s?(Jr.|Sr.))|(".+"\s?)', '', name)

Demo

关于python - 组合三个正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29356552/

相关文章:

python - Django:通过 HttpResponse 流式传输动态生成的 XML 输出

java - 正则表达式模式检查是否存在除空格字符之外的任何内容

ruby - 正则表达式中的悬挂元字符是什么?

Javascript string.replace 正则表达式

regex - 用于从对话中删除所有日期时间的正则表达式

python - Seaborn Restore 标记边缘

python - 卷积神经网络中的核和权重

python - for 循环如何评估其参数

ios - "\P{Letter}"和 NSRegularExpression

Python 版本与 Windows Server 2003 的兼容性