I wish match some line in Cisco configuration based on the destination. To make some test I made the following:
acl = 'ip route 10.5.48.0 255.255.255.0 10.242.245.65'
firewall = '10.242.245.65'
ip = r"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)"
ip_route = (f"ip route {ip} {ip} {firewall}")
if re.search(ip_route, acl):
print(acl)
No result.
I guess the problem is withe the variable ip_route
.
CodePudding user response:
Your problem is with the anchors ^
and $
in ip
:
acl = 'ip route 10.5.48.0 255.255.255.0 10.242.245.65'
firewall = '10\.242\.245\.65'
# ip = r"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)"
# ^ ^ there is your problem
# Should be:
ip = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
ip_route = (f"ip route {ip} {ip} {firewall}")
if re.search(ip_route, acl):
print(acl)
Those anchors assert beginning and end of the string respectively; you are using them in the middle of your constructed match.
Also (but not affecting this match) you should escape the .
in firewall
-- otherwise that regex metacharacter matches any character -- not just a dot.
CodePudding user response:
The ip
regex contains ^
and $
, which mean beginning and end of string.
But then ip
is inserted in the middle of ip_route
which is then used in re.search
.
So you search for "something, beginning of string, something, end of string, something", which doesn't match acl
because it doesn't contain "beginning of string" and "end of string" in the middle.
So: remove ^
and $
from ip
.