0

I'm trying to capture all the lines between "router bgp" and the last "exit-address-family" using a positive lookabehind but the match only results in capturing up to the first "exit-address-family." How do I make the lookbehind greedy to capture up to the last instance?

import re

config ='''
router bgp 400 
 bgp log-neighbor-changes
 neighbor 10:1::19 remote-as 12000
 neighbor 10.1.0.2 remote-as 400
 !
 address-family ipv4
  network 2.0.1.0 mask 255.255.255.0
  neighbor 10.1.0.38 activate
 exit-address-family
 !
 address-family ipv6
  neighbor 10:1::19 activate
 exit-address-family
!
stuff i don't want
more stuff i don't want
'''
rx_bgp_config =  re.findall(r'router\s+bgp\s+(\S+)(.*?)(?<=exit-address-family)', config, re.S|re.M)

bgp_config = rx_bgp_config[0]

print(bgp_config)
lmsherman
  • 57
  • 4

1 Answers1

1

Check this link: https://learn.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions#greedy-and-lazy-quantifiers

A non-greedy quantifier tries to match an element as few times as possible. You can turn a greedy quantifier into a lazy quantifier by simply adding a ?.

In your case, you should remove the ? to make it search all the way to the last match case. Changing your pattern from

router\s+bgp\s+(\S+)(.*?)(?<=exit-address-family)

to

router\s+bgp\s+(\S+)(.*)(?<=exit-address-family)

would do the job.

Cabara
  • 336
  • 4
  • 12