替换从 (color to ;) 开始到 ?到 )

Replace the all content starting from (color to ;) and starting ? to )

到目前为止,这是我的代码:

import re
a = ["abc", " this is in blue color","(Refer: '(color:rgb(61, 142, 185); )Set the TEST VIN value'(color:rgb(0, 0, 0); ) in document: (color:rgb(61, 142, 185); )[UserGuide_Upgrade_2020_W10_final.pdf|CB:/displayDocument/UserGuide_Upgrade_2020_W10_final.pdf?task_id=12639618&artifact_id=48569866] )"]
p = re.compile(r'(color[\w]+\;)').sub('', a[i])
print(p)

需要输出:

["abc", " this is in blue color","(Refer: 'Set the TEST VIN value' in document: [UserGuide_Upgrade_2020_W10_final.pdf|CB:/displayDocument/UserGuide_Upgrade_2020_W10_final.pdf)"]

要删除字符串中的 3 个颜色部分以及从问号末尾到 )

之前的部分

您可以使用 alternation |

匹配所有部分
\(color:\w+\([^()]*\); \)|\?[^?]+(?=\)$)

Regex demo | Python demo

  • \(color: 匹配 (color:
  • \w+\([^()]*\); \) 匹配 1+ 个单词字符,然后匹配从 () 一个 space 和另一个 )
  • |
  • \?[^?]+ 匹配 ? 和 1+ 次除 ?
  • 之外的所有字符
  • (?=\)$) 断言右边的是字符串末尾的)

示例代码

import re

regex = r"\(color:\w+\([^()]*\); \)|\?[^?]+(?=\)$)"
test_str = " this is in blue color\",\"(Refer: 'Set the TEST VIN value' in document: [UserGuide_Upgrade_2020_W10_final.pdf|CB:/displayDocument/UserGuide_Upgrade_2020_W10_final.pdf)"
result = re.sub(regex, "", test_str)

print (result)

输出

 this is in blue color","(Refer: 'Set the TEST VIN value' in document: [UserGuide_Upgrade_2020_W10_final.pdf|CB:/displayDocument/UserGuide_Upgrade_2020_W10_final.pdf)