0

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
Ahmad_R
  • 62
  • 1
  • 8
  • 1
    The regex `r"(\S+) out$"` will capture a single extent of non-space characters followed by " out" at the end of the line. – tdelaney Aug 18 '20 at 00:40
  • how to store the (\S+) which is going to be for example grn200_NWK into a variable, keep in mind i dont wanna touch 'out' i just want to find the single word right behind it – Ahmad_R Aug 18 '20 at 00:44
  • 1
    @Ahmad_R in regex, you use the parenthesis `()` to create a _capture group_ that will be basically used as a variable, that's why tdelaney used them. Look for python documentation on how use regex capture groups in docs or for instance here : https://stackoverflow.com/questions/48719537/capture-groups-with-regular-expression-python – Pac0 Aug 18 '20 at 00:51
  • 1
    @Ahmad_R - I wrote an answer that I hope makes it more clear. – tdelaney Aug 18 '20 at 00:52
  • @tdelaney thank you so much let me test it and will let you know – Ahmad_R Aug 18 '20 at 02:16

2 Answers2

1

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.

hedy
  • 1,160
  • 5
  • 23
0

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():]
tdelaney
  • 73,364
  • 6
  • 83
  • 116