I'm trying to extract a headers between the two dotted line
------------------------------------------------------------------------------------------
PortNo Descr Status Speed Mode Pause FlowCtrl
------------------------------------------------------------------------------------------
1 A UP 200G FULL NO YES
I wrote this regular expression to get the headers alone
header = re.search(r'--+\n(.*)\n--+', cli_output).group(1)
I tried to use the regex
--+\n(.*)
which picks the 2 and 4th line. To restrict picking up the 4th line, I modified the regex as
--+\n(.*)\n--+
Now it does not match anything. Where am I going wrong here? I tried in https://regex101.com/ as well but it does not match.
Your regex requires that the second line of hyphens starts at the beginning of its line, but that is not the case: there are spaces in front of those hypens.
So replace that second \n
with \s+
:
re.search(r'--+\n(.*)\s+--+', cli_output)
Note that you shouldn't chain .group(1)
immediately to that search
call. It is good practice to first check whether you have a match or not:
match = re.search(r'--+\n(.*)\s+--+', cli_output)
if not match:
print("there was no match of a heading")
else:
header = match.group(1)
print(header)