Find and replace with Regex in Python

Hi everyone,

i have the lines below, and would like to find a specific word (the bold text)

neighbor 10.1.1.1 route-map wred900_NWK out
neighbor 10.1.1.1 route-map grn_NWK out
neighbor 10.1.1.1 route-map blu_NWK out

In principle you can use a capturing group (the part in parentheses) in the regular expression to capture specific parts of the match. The exact expression will of course depend on what exactly you want to match, in the example below the capturing group will simply match the last word before “out”.

import re
r = re.compile(r'.*\s(\w+)\sout')
m = r.match(line)
m.group(1)

If you want to replace anything you’ll need to explain some more what you want to do.