0

Let's say I have the following text

bridge-domain 30 
bridge-domain 35 
bridge-domain 40 
bridge-domain 42 
bridge-domain 44 
bridge-domain 54 
 member GigabitEthernet0/0/7 service-instance 54
 member GigabitEthernet0/0/8 service-instance 54
 member GigabitEthernet0/0/10 service-instance 54
 member GigabitEthernet0/0/11 service-instance 54
 member vfi VLAN54
!
bridge-domain 61 
bridge-domain 88 
bridge-domain 93 
 member GigabitEthernet0/0/5 service-instance 93
 member GigabitEthernet0/0/8 service-instance 93
 member vfi VLAN93
!

I want the bridge-domain sections, so i use the following regex:

bridge_domain_sections = re.findall(r"\n(bridge-domain\s\d+.*?\n!)", source, re.M|re.S)

I get,

['bridge-domain 30 \nbridge-domain 35 \nbridge-domain 40 \nbridge-domain 42 \nbridge-domain 44 \nbridge-domain 54 \n member GigabitEthernet0/0/7 service-instance 54\n member GigabitEthernet0/0/8 service-instance 54\n member GigabitEthernet0/0/10 service-instance 54\n member GigabitEthernet0/0/11 service-instance 54\n member vfi VLAN54\n!', 'bridge-domain 61 \nbridge-domain 88 \nbridge-domain 93 \n member GigabitEthernet0/0/5 service-instance 93\n member GigabitEthernet0/0/8 service-instance 93\n member vfi VLAN93\n!']

whereas I need just the sections which start with "bridge-domain ... ending with !"

['\nbridge-domain 54 \n member GigabitEthernet0/0/7 service-instance 54\n member GigabitEthernet0/0/8 service-instance 54\n member GigabitEthernet0/0/10 service-instance 54\n member GigabitEthernet0/0/11 service-instance 54\n member vfi VLAN54\n!', 'bridge-domain 93 \n member GigabitEthernet0/0/5 service-instance 93\n member GigabitEthernet0/0/8 service-instance 93\n member vfi VLAN93\n!']

I know its greedily matching the start, not sure how to correct this, any help to understand this is much appreciated

5

1 Answer 1

1

You could match bridge-domain followed by all lines that do not start with bridge-domain or an ! and then match the first line that only has !

Note that using \s can also match a newline. If you want to match multiple whitespace chars except a newline, you can use [^\S\r\n]* instead.

^bridge-domain \d+ *(?:\r?\n(?!bridge-domain \d|!$).*)*\r?\n!$
  • ^ Start of string
  • bridge-domain \d+ * Match bridge-domain, a space and 1+ digits plus optional spaces
  • (?: Non capture group
    • \r?\n(?!bridge-domain \d|!$).* Match a newline and the whole line if it does not start with either bridge-domain and a digit or only !
  • )* Close non capture group and optionally repeat (Or use + if there has to be at least a single line)
  • \r?\n! Match a newline and !
  • $ End of string

Regex demo

Or match bridge-domain and only check that the next line does not start with bridge-domain and match as least as possible lines until the first line that only has !

^bridge-domain \d+ *\r?\n(?!bridge-domain \d)(?:.*\r?\n)*?!$
  • ^ Start of string
  • bridge-domain \d+ *\r?\n Match bridge-domain, 1+ digits, optional whitspace chars and a newline
  • (?!bridge-domain \d) Negative lookahead to assert that the string does not start with bridge-domain followed by a space and digit
  • (?:.*\r?\n)*? Repeat as least as possible lines followed by a newlin
  • ! Match !
  • $ End of string

Regex demo

Note that this pattern can match a bridge-domain line if it is between a member GigabitEthernet, as the first lookahead only asserts that from that location bridge-domain is not directly to the right.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.