简体   繁体   中英

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

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.

my regex is: (i2.2.2.0/24).*(\\n123.2.2.106)...

I need the expression to use in python code. 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; see regex101 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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