简体   繁体   English

Python正则表达式匹配多行文本块

[英]Python regex match multiline block of text

I wrote an regex to select the first block, but it doesn't work. 我编写了一个正则表达式来选择第一个块,但是它不起作用。 It just select i2.2.2.0 and when I want to add another string it doesn't have any 它只是选择i2.2.2.0 ,当我想添加另一个字符串时,它没有任何内容

My input is: 我的输入是:

> 1.1.1.0/24       0.0.0.0                  0         32768 i
* i2.2.2.0/24       2.2.2.2                  0    100      0 i
*>i                 123.2.2.106              0    100      0 i
*>i                 123.1.1.106              0    100      0 i
* i3.3.3.0/24       123.3.3.107              0    100      0 i
* i                 123.3.3.107              0    100      0 i
* i123.1.1.0/24     2.2.2.2                  0    100      0 i
* i                 123.2.2.106              0    100      0 i
*>                  0.0.0.0                  0         32768 i
* i123.2.2.0/24     2.2.2.2                  0    100      0 i
* i                 123.2.2.106              0    100      0 i
*>                  0.0.0.0                  0         32768 i
* i123.3.3.0/24     2.2.2.2                  0    100      0 i
*>i                 123.2.2.106              0    100      0 i
*>i                 123.1.1.106              0    100      0 i

my results should be: 我的结果应该是:

  1. * i2.2.2.0/24 2.2.2.2 0 100 0 i *>i 123.2.2.106 0 100 0 i *>i 123.1.1.106

  2. i123.3.3.0/24 2.2.2.2 0 100 0 i *>i 123.2.2.106 0 100 0 i *>i 123.1.1.106 result. i123.3.3.0/24 2.2.2.2 0 100 0 i *>i 123.2.2.106 0 100 0 i *>i 123.1.1.106结果。

my regex is: (i2.2.2.0/24).*(\\n123.2.2.106)... 我的正则表达式是: (i2.2.2.0/24).*(\\n123.2.2.106)...

I need the expression to use in python code. 我需要在python代码中使用的表达式。 I searched a lot but Ican't find the result. 我搜索了很多,但找不到结果。

Thanks :) 谢谢 :)

You want to match blocks belonging to selected network addresses. 您要匹配属于所选网络地址的块。 The continuation lines are recognizable by space in place of an address. 可以用空格代替地址来识别连续线。 The regular expression 正则表达式

(\* i(?:123.3.3.0/24|2.2.2.0/24).+\n(?:\*.i .+\n)*)

for example matches (with the global modifier) both the 2.2.2.0/24 and the 123.3.3.0/24 block in any order; 例如,以任意顺序匹配(使用全局修饰符) 2.2.2.0/24123.3.3.0/24块; see regex101 . 参见regex101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM