[英]Python regex positive lookbehind
I have some multiline text that changes each time my python program is running.我有一些多行文本,每次运行 python 程序时都会发生变化。 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
我想搜索一个搜索模式的出现,然后在后面查找以以下内容开头的第一行:
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.我想找到:
network-object object HD700041_10.88.64.141
然后当我network-object object HD700041_10.88.64.141
第一次出现object-group network
。
I tried this regex search pattern: (object-group network.+)[\\w\\W]+?(?<=HD700041_10\\.88\\.64\\.141)
我试过这个正则表达式搜索模式:
(object-group network.+)[\\w\\W]+?(?<=HD700041_10\\.88\\.64\\.141)
But the result is: object-group network CISCO_ROUTER
但结果是:
object-group network CISCO_ROUTER
How do I find the first occurrence object-group network CISCO_SWITCH
when looking behind? object-group network CISCO_SWITCH
时如何找到第一个出现的object-group network CISCO_SWITCH
?
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 (?m)^
- 一行的开始(object-group network.*)
- Group 1: the line with object-group network
at the start (object-group network.*)
- Group 1:以object-group network
开头的那条线(?:\\n(?!object-group).*)*
- zero or more lines that do not start with object-group
string (?:\\n(?!object-group).*)*
- 零个或多个不以object-group
字符串开头的行\\n
- a newline \\n
- 换行.*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. .*HD700041_10\\.88\\.64\\.141
- 除换行符以外的任何零个或多个字符,尽可能多,然后是HD700041_10.88.64.141
文本。 Note that 141
at the end may also match 141
in 141000
.需要注意的是
141
在年底也可搭配141
在141000
。 Add (?!\\d)
at the end if you want to match specifically 141
value.如果要特别匹配
141
值,请在末尾添加(?!\\d)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.