如何使用 python 正则表达式在匹配后获取下一行

问题描述 投票:0回答:1

我对 python 比较陌生,正在编写代码以在 python 正则表达式匹配后到达下一行。 我查看了诸如 https://regex101.com/r/dQ0gR6/1after match, get next line in a file using python bit 无法实现我想要捕获的示例。

我的文字是:

FlowRole  SrcAddr        DstAddr        Proto   SrcPort  DstPort  Key Type Vrf   Dst Vrf  Service Instance Session Id Src Local Dst Local CP Redirect NH type   NH id
I         172.16.10.1    172.18.10.1    ICMP    21       2048     1        2     3        1                8389614    1         0         0           2         1
FlowRole  SrcAddr        DstAddr        Proto   SrcPort  DstPort  Key Type Vrf   Dst Vrf  Service Instance Session Id Src Local Dst Local CP Redirect NH type   NH id
R         172.18.10.1    172.18.10.110  ICMP    21       0        1        3     2        1                8389614    0         1         0           2         1

我用来匹配的代码是:

FlowRole[^\n]+([\r\n]\w+)    --> I;m able to match the line and the next line word but not the entire line..

这里需要一些帮助。

我想在模式匹配后匹配整行

即,我想匹配这两行:

I         172.16.10.1    172.18.10.1    ICMP    21       2048     1        2     3        1                8389614    1         0         0           2         1
R         172.18.10.1    172.18.10.110  ICMP    21       0        1        3     2        1                8389614    0         1         0           2         1
python-3.x regex
1个回答
0
投票

你已经完成了 90%,我只是更改了

\w+
(它只查找单词
[^\r\n]
,就像你之前用来匹配整行的那样)-

FlowRole[^\n]+[\r\n]([^\r\n]+)

这给出了 regex101 的预期输出

© www.soinside.com 2019 - 2024. All rights reserved.