使用正则表达式删除单词

Delete words using regular expressions

如何从文本中删除 SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed. 行?

text = (
  'SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed.HP-UX B.11.00, B.11.11, and B.11.22.',
  'SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed.HP-UX B.11.00, B.11.04, B.11.11 running',
  'SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed.HP-UX B.11.11 and B.11.23'
)

您不需要正则表达式,只需一个简单的 replace 函数:

text = tuple(item.replace("SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed.", "") 
       for item in text)

您可以使用 replace,因为它更简单。

text = text.replace("SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed.", "")

但是如果你真的想要一个正则表达式为你完成这项工作(我不建议在这种特殊情况下这样做),你可以这样做:

text = "SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed.HP-UX B.11.00, B.11.11, and B.11.22."
text = re.sub("SUPPORTED SOFTWARE VERSIONS\*: ONLY impacted versions are listed\.", "", text)
# 'HP-UX B.11.00, B.11.11, and B.11.22.'

您可以在python中使用字符串替换功能。 documentation

toRemove = "SUPPORTED SOFTWARE VERSIONS*: ONLY impacted versions are listed."
modifiedText = tuple(map(lambda x: x.replace(toRemove, ''), text))

注意:您可以使用re库中的替换函数来使用正则表达式。