regex - Python regular expression to search a string -
i've below code:
def ospf_route_type_e2_exists(nbr_output,as_ext_net_list,net_list): match_list=[] if type(as_ext_net_list) not list: as_ext_net_list=[as_ext_net_list] if type(net_list) not list: net_list=[net_list] ext_net,net in zip(as_ext_net_list,net_list): net_prefix=str(net).rstrip(".0") match= re.search(r"o[" "]+e2[" "]+str\(ext_net\)[" "]+\[[0-9]*[0-9]*[0-9]+\/[0-9]+\][" "]+via[" "]+str(net_prefix)",nbr_output) match_list.append(match) if any(item item in match_list): print "as external networks advertised via correct areas" return 1 else: print "as external networks not advertised via correct areas" return 0
the nbr_ouput contain below:
show ip route ospf o ia 192.168.42.0/24 [110/2] via 192.168.43.4, 00:00:29, fastethernet0/0 o 192.168.124.0/24 [110/2] via 192.168.43.4, 00:00:29, fastethernet0/0 10.0.0.0/24 subnetted, 1 subnets o e2 10.1.1.0 [110/1] via 192.168.43.4, 00:00:29, fastethernet0/0
i want search line:
o e2 10.1.1.0 [110/1] via 192.168.43.4, 00:00:29, fastethernet0/0
using match line in code:
match= re.search(r"o[" "]+e2[" "]+str\(ext_net\)[" "]+\[[0-9]*[0-9]*[0-9]+\/[0-9]+\][" "]+via[" "]+str(net_prefix)",nbr_output)
expected output:
print "as external networks advertised via correct areas" return 1
output get:
print "as external networks not advertised via correct areas" return 0
which tells me regular expression search doesn't work. please let me know doing wrong
i used:
match= re.search(r"o\s+e2\s+"+str(ext_net)+"\s+\[[0-9]+\/[0-9]+\]\s+via\s+"+str(net_prefix),nbr_output)
and works.
Comments
Post a Comment