蟒蛇 - re.DOTALL 产生"None"输出



我正试图从以下代码中获取2个ipv6地址:

import re
from pprint import pprint
with open('show_ipv6_intf.txt') as f:
show_ipv6_int = f.read()
match = re.search(r'^s+(S+)s[VALID]s+(S+).*', show_ipv6_int,flags=re.M)
ipv6_1 = match.group(1)
ipv6_2 = match.group(2)
ipv6_list = []

ipv6_list.append(ipv6_1)
ipv6_list.append(ipv6_2)
print(ipv6_list)

对于宽度,

这是正在使用的文件:

Ethernet2/4, Interface status: protocol-up/link-up/admin-up, iod: 40
IPv6 address:
2001:11:2233::a1/24 [VALID]
2001:cc11:22bb:0:2ec2:60ff:fe4f:feb2/64
IPv6 subnet:  2001::/24
IPv6 link-local address: fe80::2ec2:60ff:fe4f:feb2 (default) [VALID]
IPv6 virtual addresses configured: none
IPv6 multicast routing: disabled
IPv6 report link local: disabled
IPv6 Forwarding feature: disabled
IPv6 multicast groups locally joined:
ff02::1:ff4f:feb2  ff02::2  ff02::1  ff02::1:ff00:a1
ff02::1:ff4f:feb2  ff02::1:ff00:0
IPv6 multicast (S,G) entries joined: none
IPv6 MTU: 1500 (using link MTU)
IPv6 unicast reverse path forwarding: none
IPv6 load sharing: none
IPv6 interface statistics last reset: never
IPv6 interface RP-traffic statistics: (forwarded/originated/consumed)
Unicast packets:      0/0/0
Unicast bytes:        0/0/0
Multicast packets:    0/18/0
Multicast bytes:      0/2076/0

如果我使用标志";关于M";我检索期望的结果,但当我使用";关于"DOTALL";我将";无";布尔值。见下文:

带有re.M

C:UsersKenyone.PyCharm2019.3configscratches>python week4lesson6.py ['2001:11:2233::a1/24', '2001:cc11:22bb:0:2ec2:60ff:fe4f:feb2/64']

与re.DOTALL

C:UsersKenyone.PyCharm2019.3configscratches>python week4lesson6.py Traceback (most recent call last): File "week4lesson6.py", line 7, in <module> ipv6_1 = match.group(1) AttributeError: 'NoneType' object has no attribute 'group'

您希望从re.DOTALL获得什么?它改变了正则表达式模式中.的行为,但您只有一个,没有必要。由于后面跟有*,所以它可以匹配零次。你可以在没有点星的情况下重写模式,得到同样的行为。

r'^s+(S+)s[VALID]s+(S+)'应该是相同的并且没有点。

re.M是必要的,因为您有^,并且希望它在一行的开头匹配,而不仅仅是在整个字符串的开头。

你可以同时使用这两个标志,它会起作用,但re.DOTALL在这里没有做任何有用的事情。

如果不使用行断言,则不需要re.M

[ t]*([d:a-f/]+)[ t]*[VALID]s+([d:a-f/]+)

演示

但这可能会使正则表达式速度变慢

有了行断言,更快,[VALID]两侧的字符类更具体:

演示2

Python演示(txt设置为示例文本(:

>>> reg=re.compile(r'^[ t]*([d:a-f/]+)[ t]*[VALID]s+([d:a-f/]+)', flags=re.M)
>>> reg.findall(txt)
[('2001:11:2233::a1/24', '2001:cc11:22bb:0:2ec2:60ff:fe4f:feb2/64')]

使用YOUR regex或THIS regex,您不需要re.S,它是使.n匹配的标志。事实上,如果添加re.S,您的正则表达式将只匹配第一个匹配项,因为.*现在将匹配到字符串的末尾:

演示3

最新更新