Home > Net >  Python regex positive lookbehind
Python regex positive lookbehind

Time:10-27

I have some multiline text that changes each time my python program is running. I want to search for an occurrence of a search pattern and then lookbehind to find the first line that starts with: object-group network

Example text (can be several thausands of lines long):

object-group network CISCO_ROUTER
 network-object host 1.1.1.1
 network-object object NCPVGW03_10.1.1.2
 network-object object NCPVGW04_10.1.1.3
 network-object object SGAVGW01_10.2.2.2
 network-object object NPLVGW02_10.1.6.2
 network-object object NCPVGW02_10.1.1.3
 network-object object C1121-8P_FGL2418L267_10.8.8.1
 network-object object NDEVGW01_10.6.4.2
 network-object object HD999901_192.168.0.3
 network-object object ISR4321-FDO21172C94_10.8.8.2
 network-object object DMRVGW02_10.1.1.4
object-group network CISCO_SWITCH
 network-object host 1.1.1.1
 network-object object HD138203_10.198.80.3
 network-object object HD165103_10.5.0.1
 network-object object HD166207_10.5.1.7
 network-object object HD134402_10.194.176.102
 network-object object HD137602_10.196.176.102
 network-object object HD131603_10.192.240.103
 network-object object HD134104_10.194.128.104
 network-object object HD166503_10.53.192.103
 network-object object HD165510_10.53.64.110
 network-object object HD202001_10.33.48.30
 network-object object HD132706_10.193.160.106
 network-object object HD700041_10.88.64.141

I want to find: network-object object HD700041_10.88.64.141 and then the first occurrence of object-group network when i lookbehind.

I tried this regex search pattern: (object-group network. )[\w\W] ?(?<=HD700041_10\.88\.64\.141)

But the result is: object-group network CISCO_ROUTER

How do I find the first occurrence object-group network CISCO_SWITCH when looking behind?

CodePudding user response:

You can use

(?m)^(object-group network.*)(?:\n(?!object-group).*)*\n.*HD700041_10\.88\.64\.141

See the regex demo. Details:

  • (?m)^ - start of a line
  • (object-group network.*) - Group 1: the line with object-group networkat the start
  • (?:\n(?!object-group).*)* - zero or more lines that do not start with object-group string
  • \n - a newline
  • .*HD700041_10\.88\.64\.141 - any zero or more chars other than line break chars, as many as possible, and then HD700041_10.88.64.141 text.

Note that 141 at the end may also match 141 in 141000. Add (?!\d) at the end if you want to match specifically 141 value.

  • Related