gpt4 book ai didi

python - 单独提取IPV6地址的正则表达式

转载 作者:行者123 更新时间:2023-12-01 03:50:56 26 4
gpt4 key购买 nike

我正在尝试从以下行中提取 IPV6 地址。我得到以下输出..我只想直到 IPV6 地址,而不是超出该地址。那是我不想看到 Scope:Link

代码:

out ='ifconfig eth6.36\r\neth6.36   Link encap:Ethernet  HWaddr A0:36:9F:5F:24:EE  \r\n          inet addr:36.36.36.10  Bcast:36.36.36.255  Mask:255.255.255.0\r\n          inet6 addr: fe80::a236:9fff:fe5f:24ed/64  Scope:Link\r\n          inet6 addr: 2030:30:30:30::10/64  Scope:Link\r\n  UP BROADCAST MULTICAST  MTU:9000  Metric:1\r\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\r\n          TX packets:62 errors:0 dropped:0 overruns:0 carrier:0\r\n          collisions:0 txqueuelen:0 \r\n          RX bytes:0 (0.0 b)  TX bytes:7004 (6.8 KiB)\r\n\r\n'
pattern = '(eth6\.36\s+Link encap:Ethernet\s*HWaddr\s*\w\w:\w\w:.+\s*inet addr:36.36.36.10\s*Bcast:36.36.36.255\s*Mask:255.255.255.0\s*\inet6 addr:\s*\w\w\w\w:.+)'

match = re.search(pattern, out)
print match.groups()

输出:

('eth6.36   Link encap:Ethernet  HWaddr A0:36:9F:5F:24:EE  \r\n          inet addr:36.36.36.10  Bcast:36.36.36.255  Mask:255.255.255.0\r\n          inet6 addr: fe80::a236:9fff:fe5f:24ed/64  Scope:Link\r',)

最佳答案

您可以根据需要移动括号。喜欢

pattern = '(eth6\.36\s+Link encap:Ethernet\s*HWaddr\s*\w\w:\w\w:.+\s*inet addr:36.36.36.10\s*Bcast:36.36.36.255\s*Mask:255.255.255.0\s*\inet6 addr:\s*\w\w\w\w:.+)  Scope:Link'

将会得到

In [9]: match.groups()[0]
Out[9]: 'eth6.36 Link encap:Ethernet HWaddr A0:36:9F:5F:24:EE \r\n inet addr:36.36.36.10 Bcast:36.36.36.255 Mask:255.255.255.0\r\n inet6 addr: fe80::a236:9fff:fe5f:24ed/64'

pattern = 'eth6\.36\s+Link encap:Ethernet\s*HWaddr\s*\w\w:\w\w:.+\s*inet addr:36.36.36.10\s*Bcast:36.36.36.255\s*Mask:255.255.255.0\s*\inet6 addr:\s*(\w\w\w\w:.+)  Scope:Link'

将会得到

In [16]: match.groups()[0]
Out[16]: 'fe80::a236:9fff:fe5f:24ed/64'

关于python - 单独提取IPV6地址的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com