python - 正则表达式(Python)-匹配MAC地址

标签 python regex mac-address regular-language

我有这个文本(来自 ipconfig/all)

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :    
   Description . . . . . . . . . . . : Atheros AR8151 PCI-E Gigabit Ethernet    Controller (NDIS 6.20)   
   Physical Address. . . . . . . . . : 50-E5-49-CE-FC-EF   
   DHCP Enabled. . . . . . . . . . . : Yes   
   Autoconfiguration Enabled . . . . : Yes    
   Link-local IPv6 Address . . . . . : fe80::5cba:e9f2:a99f:4499%11(Preferred)    
   IPv4 Address. . . . . . . . . . . : 10.0.0.1(Preferred)    
   Subnet Mask . . . . . . . . . . . : 255.255.255.0   
   Lease Obtained. . . . . . . . . . : ™ 06 €…‚…‘ˆ 2016 20:35:49   
   Lease Expires . . . . . . . . . . : ‰…™‰™‰ 09 €…‚…‘ˆ 2016 21:05:49   
   Default Gateway . . . . . . . . . : 10.0.0.138   
   DHCP Server . . . . . . . . . . . : 10.0.0.138   
   DHCPv6 IAID . . . . . . . . . . . : 240182601   
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-7A-1F-FC-50-E5-49-CE-FC-EF   
   DNS Servers . . . . . . . . . . . : 10.0.0.138   
   NetBIOS over Tcpip. . . . . . . . : Enabled    

Ethernet adapter Local Area Connection* 11:   

   Description . . . . . . . . . . . : Juniper Networks Virtual Adapter    
   Physical Address. . . . . . . . . : 02-05-85-7F-EB-80     
   DHCP Enabled. . . . . . . . . . . : No    
   Autoconfiguration Enabled . . . . : Yes    
   Link-local IPv6 Address . . . . . : fe80::8dfb:6d42:97e1:2dc7%19(Preferred)     
   IPv4 Address. . . . . . . . . . . : 172.16.2.7(Preferred)      
   Subnet Mask . . . . . . . . . . . : 255.255.255.255     
   Default Gateway . . . . . . . . . :       
   DHCPv6 IAID . . . . . . . . . . . : 436340101     
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-19-7A-1F-FC-50-E5-49-CE-FC-EF    
   DNS Servers . . . . . . . . . . . : 172.16.0.6     
                                       172.16.0.91     
   NetBIOS over Tcpip. . . . . . . . : Enabled     

我只想匹配物理地址 (MAC)

从这里开始将是 2:
50-E5-49-CE-FC-EF02-05-85-7F-EB-80

这个 ( link ) ([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}) 不起作用,因为
这也将00-01-00-01-19-7A-1F-FC-50-E5-49-CE-FC-EF
所以我将其修复为这个 ([^-])([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2 })([^-]) (强制在开始和结束时不使用 -)
但现在我进入了马赫开头的空白,最后我得到了 /n
这是链接:https://regex101.com/r/kJ7mC0/1

我需要一个优雅 正则表达式来解决它。

最佳答案

您得到非 -两边都有符号,因为否定字符类 [^-]匹配并消耗字符。为了避免在匹配值中获取这些字符,您可以使用lookarounds:

p = re.compile(r'(?<!-)(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2}(?!-)', re.IGNORECASE)
                 ^^^^^^                                  ^^^^

(?<!-)负向回顾确保没有 -在您需要提取的值之前,以及 (?!-)是一个否定的前瞻,如果存在 - 则匹配失败在此值之后。

如果:不需要分隔符,替换 [:-]- 。与 re.findall 一起使用.

此外,所有 (...)应转向(?:...)非捕获组,以免“破坏”比赛结果。

请参阅Python demo

另一种选择是使用正则表达式,其中 1 个捕获组捕获我们需要的内容,并匹配我们不需要的内容:

p = re.compile(r'(?:^|[^-])((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})(?:$|[^-])', re.IGNORECASE)

它看起来不太优雅,但工作方式与 (?:^|[^-]) 相同。非捕获组不包含在 re.findall 中结果,并匹配字符串的开头或除 - 之外的符号。 (?:$|[^-])匹配字符串结尾或 - 以外的符号。

此外,要缩短模式,您可以替换 a-fA-F只需 a-f在字符类中并使用 re.IGNORECASEre.I标志。

关于python - 正则表达式(Python)-匹配MAC地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38858244/

相关文章:

python - 使用 Selenium 从 div 获取文本

Java 正则表达式 : match multiple word boundaries

php - 如何检查字符串是否是有效的 XML 元素名称?

macos - 在 OS X 中获取 mac 地址的 Shell 命令

android - 扫描手机附近的所有wifi设备

python - Boto3 ec2 describe_instances 始终返回空

python - 为什么使用 string.__class__(string) 进行字符串赋值?

python - 简化此 python 代码

javascript - 从 JSON 中删除新行

objective-c - 获取另一台计算机的 MAC 地址,给定其 IP 地址