You could make use of the PyPi regex module and the \G
anchor and a capturing group which which will be returned by using regex.findall.
(?:host-vpp1out .*|\G(?!^))\n\s+L3\s+(\d[\d.:\/a-z]+)\/\d+
(?:
Non capture group
host-vpp1out.*
Match host-vpp1out and the rest of the line
|
Or
\G(?!^)
Assert the position at the previous match, not at the start
)
Close non capture group
\n\s+
Match a newline and 1+ whitespace chars
L3\s+
Match L3
and 1+ whitespace chars
(
Capture group 1
\d[\d.:\/a-z]*
Match a digit followed by 1+ times any for the listed
)
Close group 1
\/\d+
Match /
and 1+ digits
Regex demo | Python demo
Note that this part (\d[\d.:\/a-z]+)\/\d+
is a broad match to match an ipv4 or ipv6 pattern. The links contain pages with a more specific pattern.
Example code
import regex
pattern=r"(?:host-vpp1out.*|\G(?!^))\n\s+L3\s+(\d[\d.:\/a-z]*)\/\d+"
test_str = ("VirtualFuncEthernet0/7/0.2001 (up):\n"
" L3 1.1.2.2/24 ip4 table-id 8 fib-idx 2\n"
" L3 2001:db8:0:1:1:1:2:2/112 ip6 table-id 8 fib-idx 1\n"
"VirtualFuncEthernet0/9/0 (dn):\n"
"host-vpp1out (up):\n"
" L3 1.1.1.1/24\n"
" L3 1.1.2.1/24\n"
" L3 2001:db8:0:1:1:1:1:1/112\n"
" L3 2001:db8:0:1:1:1:2:1/112\n"
"local0 (dn):\n"
"loop0 (up):\n"
" L3 1.1.1.1/32 ip4 table-id 7 fib-idx 1")
print(regex.findall(pattern, test_str))
Output
['1.1.1.1', '1.1.2.1', '2001:db8:0:1:1:1:1:1', '2001:db8:0:1:1:1:2:1']
Using re instead of regex, you could also do it in 2 steps, first matching host-vpp1out
and the L3 lines. Then from that match, you can get the values in group 1 using re.findall.
import re
regex=r"^host-vpp1out .*(?:\r?\n[^\S\r\n]*L3 .*)*"
test_str = ("VirtualFuncEthernet0/7/0.2001 (up):\n"
" L3 1.1.2.2/24 ip4 table-id 8 fib-idx 2\n"
" L3 2001:db8:0:1:1:1:2:2/112 ip6 table-id 8 fib-idx 1\n"
"VirtualFuncEthernet0/9/0 (dn):\n"
"host-vpp1out (up):\n"
" L3 1.1.1.1/24\n"
" L3 1.1.2.1/24\n"
" L3 2001:db8:0:1:1:1:1:1/112\n"
" L3 2001:db8:0:1:1:1:2:1/112\n"
"local0 (dn):\n"
"loop0 (up):\n"
" L3 1.1.1.1/32 ip4 table-id 7 fib-idx 1")
match = re.search(regex, test_str, re.MULTILINE)
if match:
print(re.findall(r" L3 (\d[\d.:\/a-z]+)\/\d+", match.group()))
Python demo