[英]finding a word on the following line python
我在一个文本文件中搜索某个字符串,然后寻找该字符串后面的另一个字符串,它可能在下一行或文档的更下方。 我目前有
所以一个示例文本 output 想
there is a word1. then there is some more text.
then we are looking for word2 = apple.
我正在寻找返回单词'apple'+ word1。 但是 word2= 可以在下一行或文档的更下方。 我已经设法做到以下几点,但这只有在下一行时才有效。 如果它在 3,4、5 等线上,没有人可以帮忙吗?
if 'word1' in line and 'word2' not in line:
nextLine = next(f)
pattern = re.match('(?:word2=|word2 =)([a-z0-9_])+',nextLine)
if pattern:
print('word1', pattern)
如果我做对了,我为你做了一个例子:
string = """
there is a word1. then there is some more text.
then we are looking for word2 = apple.
there is a word1. then there is some more text.
then we are looking for word2 = orange.
there is a word1. then there is some more text.
then there is some more text.
then there is some more text.
then we are looking for word2= peer.
"""
import re
result = re.findall(".*?(word1)[\s\S]*?word2 *=.*?([a-z0-9_]+)", string)
print(result)
# should be [('word1', 'apple'), ('word1', 'orange'), ('word1', 'peer')]
注意:由于我使用整个字符串进行匹配,我的示例可能不适合大文件。
if 'word1' in line and 'word2' not in line:
while True:
nextLine = next(f)
pattern = re.match('(?:word2=|word2 =)([a-z0-9_])+',nextLine)
if pattern:
print('word1', pattern)
break
不确定它是否会工作 无法访问 PC 让我知道,如果不工作我会删除它
当心强硬:
您应该在一个字符串中读取完整的文件,然后试试这个。 这将捕获 word1,以及使用捕获组等同于 word2 的任何内容:
(word1)(?:.*[\n\r]?)+word2 ?= ?(\w+)
从您的问题中不清楚我们是否应该匹配word2 = apple
或word2=apple
(也许您上次提到word2=
这是一个错字?),所以我包括了?
字符,这将使空格可选。
如果您希望以apple + word1
格式给出答案,您可以执行以下操作:
print(pattern.group(1) + " + " + pattern.group(2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.