Ahmad_R
Ahmad_R

Reputation: 62

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

i am very new to regex world. what i have is a text file which i would like to find a specific word before a specific string (in this case 'out') in it and store into a variable. so i can replace it with something else later in the code. below i will put < > around the interesting words i am looking for just for the highlighting purpose. it would be awesome if someone can point me to the right direction. the text file that i have is as below: in this case i would like to find the words that are highlighted with < >. i apologize in advance if my question is not as clear as i have difficulty describing what i am looking for.

  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

Upvotes: 0

Views: 1424

Answers (2)

hedy
hedy

Reputation: 1208

Assuming your file name is file.txt, you can fetch all the lines, and use regex to get all the data you need.

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)

Result:

['blu330_NWK', 'grn200_NWK']

This will print all the results you want by getting all the lines in the file, and then cycle through it, find the text and saves in it a list called results. Then you can use this to get your values in variables.

I believe that this solution is simpler to understand as it just cycles through the lines and finds the results.

Upvotes: 1

tdelaney
tdelaney

Reputation: 77347

You can use a capture group to find the word you want. It can be slightly different depending on what constitutes a word (emojis?). Below is an expansive definition - anything without whitespace. In this example I just store the result of the search on each line. A None means no match. Otherwise its a search object where group(1) is the word found, start() is its start index and end() is itss end index. Just as an example, I change the word to "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():]

Upvotes: 0

Related Questions