Python Regex 循环非捕获组

标签 python regex

我有以下字符串:

12/27/2016 08:05 AM (GMT+01:00) added by Operator A (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/26/2016 08:06 AM (GMT+01:00) added by Operator B (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/23/2016 08:06 AM (GMT+01:00) added by Operator C (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/22/2016 08:05 AM (GMT+01:00) added by Operator A (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/22/2016 12:57 AM (GMT+01:00) added by Operator B (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/21/2016 02:03 AM (GMT+01:00) added by Operator C (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n'

如您所见,if 具有重复的短语:

12/27/2016 08:05 AM (GMT+01:00) added by Operator A/B/C (PID-000301): ...

我想通过编写一个正则表达式来清理这个重复出现的短语并将其删除。到目前为止,我已经写过类似的东西:

r'(?:\n?\d\d\/\d\d\/\d\d\d\d.+\(PID-\d{6}\):)+([\w\W]+)'

但是,这只会删除第一次出现的重复短语。我如何“循环”它,以便它拾取所有实例?

最佳答案

Regex101 :

import re

s = '12/27/2016 08:05 AM (GMT+01:00) added by Operator A (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/26/2016 08:06 AM (GMT+01:00) added by Operator B (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/23/2016 08:06 AM (GMT+01:00) added by Operator C (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/22/2016 08:05 AM (GMT+01:00) added by Operator A (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/22/2016 12:57 AM (GMT+01:00) added by Operator B (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n*************************************************************************************************\r\n12/21/2016 02:03 AM (GMT+01:00) added by Operator C (PID-000301):\r\nOthers/Malfunction;Device failure not specified;It Crasched during upgrade\r\n\r\n'

s = re.sub(r'\s*\d{2}/\d{2}/\d{4}.+\(PID-\d{6}\):', '', s)

print(s)

打印:

Others/Malfunction;Device failure not specified;It Crasched during upgrade

*************************************************************************************************
Others/Malfunction;Device failure not specified;It Crasched during upgrade

*************************************************************************************************
Others/Malfunction;Device failure not specified;It Crasched during upgrade

*************************************************************************************************
Others/Malfunction;Device failure not specified;It Crasched during upgrade

*************************************************************************************************
Others/Malfunction;Device failure not specified;It Crasched during upgrade

*************************************************************************************************
Others/Malfunction;Device failure not specified;It Crasched during upgrade

关于Python Regex 循环非捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57388548/

相关文章:

python - 使用 python 访问 Chrome DOM 树

regex - 如何从 "/test/(\d+)"等正则表达式访问捕获的值?

c# - RegEx提取前6到10位数字,不包括8位数字

regex - 尽管重定向有效,但 Nginx 位置中的匹配返回空的 $1

Java - 全名的正则表达式

javascript - 正则表达式从路径中排除文件夹

python - 如何在Python中设置一个计时器来验证用户名?

php - 引导Web应用程序-PHP,Ruby,Python,Node.js

python - 如何在 ubuntu 上创建 mysql 数据库 - 用户 '' @'localhost' 访问被拒绝

python - 尝试迁移数据库时出错(slalchemy、slqite3)