简体   繁体   English

正则表达式匹配的MAC地址

[英]Regex matching MAC address

I'm trying to get all valid MAC addresses from this string: 我正在尝试从此字符串获取所有有效的MAC地址:

00:1e:68:51:4f:a9    <-> 00:1a:8c:10:ad:30          9       540       8       336      17       876    90.457130000       198.0143

I've tried this and a few other regexes: 我已经尝试过这个和其他一些正则表达式:

^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$

Regex 101 here: 正则表达式101在这里:

https://regex101.com/r/kI5nI6/1 https://regex101.com/r/kI5nI6/1

I can't figure out why I'm not getting any matches. 我不知道为什么我没有任何比赛。

  • You have to remove the anchors ^ and $ 您必须删除锚点^$

  • You have to add az in your character set.. or make the searches case insensitive with (?i) (i modifier) 你必须添加az的字符集..或使搜索情况与不敏感(?i) (我修改)

Following will work: 以下将起作用:

([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})

See DEMO 演示

The anchors ^ and $ say to only match strings that are MAC addresses, not the parts within strings that are MAC addresses. 锚点^$表示仅匹配作为MAC地址的字符串,而不匹配作为MAC地址的字符串中的部分。 Also, your regex uses capital letters (AF), but the MAC addresses in that string are lowercase. 另外,您的正则表达式使用大写字母(AF),但该字符串中的MAC地址为小写字母。 Are you doing a case insensitive search (if using the re module, that would be re.IGNORECASE )? 您是否在进行不区分大小写的搜索(如果使用re模块, re.IGNORECASE )? Try turning on case-insensitive search or add "af" after the AF. 尝试打开不区分大小写的搜索,或者在AF之后添加“ af”。

On a side note, there is no reason to include the : in brackets ( [:] ), because that means "match any one of this one character". 另外,没有必要在括号( [:] )中包含: ,因为这意味着“匹配此字符中的任何一个”。 You can just use : directly. 您可以直接使用:

With case insensitive off you should be able to use this: 关闭不区分大小写功能后,您应该可以使用以下命令:

([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})

With case insensitive on: 在不区分大小写的情况下:

([0-9A-F]{2}:){5}([0-9A-F]{2})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM