使用 python 从文本中仅提取指定值

extracting only the specified value from text using python

我有一个包含以下数据的文本文件。我必须提取所有包含 signed

的行
The document was signed on July 12

The document was signed by Charlie

This document was assigned to John

The document was preassigned to Amanda

预期输出:

The document was signed on July 12

The document was signed by Charlie

如果我使用:

for line in file:
    if "signed" in line:
        print (line)

正在打印所有行

这很容易通过使用单词边界来完成 \b。 \bsigned 将匹配已签名但未分配的内容。

See here

您可以使用re.search(line, ".*\bsigned.*")