我尝试仅获取介于
link/ether
和brd
之间的 MAC地址。
这是我到目前为止的代码,它是一个较大文件的一部分,并包括一个示例:
import re
data = '''1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:25:b5:12:02:00 brd ff:ff:ff:ff:ff:ff '''
p = re.compile(r'(?:[0-9a-fA-F]:?){12}')
re.findall(p, line)
设法获得以下输出:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
link/ether 00:15:b8:12:09:00 brd ff:ff:ff:ff:ff:ff
问题:我尝试获取以下输出(从以
link/ether
开头的每一行中获取link/ether & brd
之间的mac地址):
00:15:b8:12:09:00
答案 0 :(得分:2)
您可以使用
p = re.compile(r'^\s*link/ether\s+([0-9a-fA-F]+(?::[0-9a-fA-F]+){5})\s+brd', re.M)
详细信息
^
-一行的开头\s*
-超过0个空格link/ether
-文字子字符串\s+
-超过1个空格([0-9a-fA-F]+(?::[0-9a-fA-F]+){5})
-捕获组1(re.findall
返回的内容):1个或更多十六进制字符,后跟5次出现的:
,再加上1个以上十六进制字符\s+
-超过1个空格brd
-brd
子字符串。请参见Python demo:
import re
data = '''1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:25:b5:12:02:00 brd ff:ff:ff:ff:ff:ff '''
p = re.compile(r'^\s*link/ether\s+([0-9a-fA-F]+(?::[0-9a-fA-F]+){5})\s+brd', re.M)
print(p.findall(data)) # => ['00:25:b5:12:02:00']