I'm parsing output from textfiles that could change depending on when script is executed. I want to capture one or more lines of ip address, advertised address, and age. When I run my script it only captures the first or last match depending on where I place the metacharacters, however I want all the ip lines to be captured. The amount of ip lines can change depending on time of day. The rest remains the same.
when I run this code the "link" capture group only captures the first or last ip line, after each "Link States" headers and column headers as a match~ it's capturing "link_state" match group just fine, and everything else is ok, however not capturing all of the ip lines ~ any suggestions on capturing them all as a group (i've included my IDLE output at the end)?
import re
temp = []
infile = ["ospf_r4.txt", "ospf_r1.txt"]
regex = (
r"(?P<link_state>[\S+ ]* +Link States) ?(?:\(Area \S+\))?\n"
r"\n*"
r"(?P<link_col>Link ID) +(?P<adv_col>ADV Router) +(?P<age_col>Age) +Seq# +(?:[\S+ ]?)+"
r"\n*"
r"**(**(?P<link>[\d+\.]+ +[\d+\.]+ +[\d]+**)**+ +[\S+ *]*[\n]*)+"
)
def temp_function(infile):
global temp
temp = []
with open(infile, "r") as x:
c = x.read()
result = re.finditer(regex, c)
for i in result:
#temp.append(i.group())
#print(i.group("link"))
print(i)
return temp
here is my output from IDLE, note instead of 8 ip's it gives me 5. Moving between finditer or findall has not worked for me. Any suggestions?
4.4.4.4 4.4.4.4 1993
172.16.14.2 4.4.4.4 1993
192.168.2.0 1.1.1.1 1031
3.3.3.3 1.1.1.1 1031
10.0.0.0 3.3.3.3 977