使用文本文件中的正则表达式在特定字符串之前查找单词

finding a word before a specific string with regex from a text file

我是正则表达式世界的新手。我有一个文本文件,我想在其中的特定字符串(在本例中为 'out')之前找到一个特定的单词并将其存储到一个变量中。所以我可以稍后在代码中用其他东西替换它。下面我将把 < > 放在我正在寻找的有趣单词周围,只是为了突出显示。如果有人能指出我正确的方向,那就太棒了。 我拥有的文本文件如下:在这种情况下,我想找到用 < > 突出显示的单词。如果我的问题不够清楚,我提前道歉,因为我很难描述我正在寻找的东西。

  neighbor 10.242.1.1 route-map LOCAL_PREF in
  neighbor 10.242.1.1 route-map <grn200_NWK> out   
  neighbor 10.244.206.2 route-map LOCAL_PREF in
  neighbor 10.244.206.2 route-map <blu330_NWK> out
  neighbor 10.242.120.202 route-map LOCAL_PREF in
  neighbor 10.242.120.202 route-map <grn200_NWK> out
.
.
.
the text file continues in this pattern

您可以使用捕获组来查找您想要的词。根据单词的构成(表情符号?),它可能会略有不同。下面是一个广泛的定义——任何没有空格的东西。在此示例中,我仅将搜索结果存储在每一行中。 None 表示不匹配。否则它是一个搜索对象,其中 group(1) 是找到的单词,start() 是它的起始索引,end() 是它的结束索引。举个例子,我把这个词改成“foo”。

import re

with open('foo.txt') as fileobj:
    searches = [(re.search(r"(\S+) out$", line.strip()) for line in fileobj]

matched = []
for match, line in searches:
    if match:
        print("matched", match.group(1))
        matched.append(line[:match.start()] + "foo" + line[match.end():]

假设您的文件名为file.txt,您可以获取所有行,并使用正则表达式获取您需要的所有数据。

import re

with open('file.txt') as f:
    contents = f.readlines() # get the lines in a list

for x in contents: # iterate through each line
    matched = re.search(r'\S+ out$', x) # find results
    if matched:
        result.append(matched.group().split(" ")[0]) # save results
print(result)

结果:

['blu330_NWK', 'grn200_NWK']

这将通过获取文件中的所有行来打印您想要的所有结果,然后循环遍历它,找到文本并在其中保存一个名为 results 的列表。然后您可以使用它来获取变量中的值。

我相信这个解决方案更容易理解,因为它只是循环遍历行并找到结果。