使用正则表达式匹配模式[关闭]

问题描述 投票:-3回答:1

我有一个字符串,我想通过使用正则表达式从整个字符串中提取一个特定的行。

这是我的字符串:

Physical interface: rt90, Enabled, Helical link is Up
Interface index: 178, SPMZ ifIndex: 980
Description: 4&109G LAG to kah7zt84
Link-level type: Ethernet, XCU: 9082, Speed: 10Gbps, BPDU Error: Hello,
Wind-REWRITE Error: None, Loopback: Disabled, Source filtering: abled,
Flow : abled
Pad to min frame size0: Disabled
Minimum li needed: 1, Minimum bandwidth need: 0bps
Device flags   : Running
Interface flags: RTYU-Traps Internal: 0x40
Current address: 1e:pb:i0:90:10:76, Hardware address: 1e:pb:i0:90:10:768
Last flapped   : 2017-12-16 32:12:12 GMT (3d 16:16 ago)
Input rate     : 115 bps (20 pps)
Output rate    : 8 bps (1 pps)`

我想提取以下行:

Physical interface: rt90, Enabled, Helical link is Up

(第一行)

有人可以帮帮忙吗?谢谢。

python regex
1个回答
1
投票

要找到以“物理接口”开头的第一行,您可以使用:

lines = text.splitlines()
for line in lines:
    if line.startswith("Physical interface"):
        print(line)
        break
else:
    print('Not found')
© www.soinside.com 2019 - 2024. All rights reserved.