用逗号分隔多个 MAC 地址的正则表达式?
我想知道匹配多个 MAC 地址的正确正则表达式分隔我的任何分隔符,例如逗号。
单个 MAC 地址的正则表达式为:^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$.
因此,对于以逗号分隔的多个 MAC 地址,我计算 ^(([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}, ?)){+}$
就可以了。
我哪里出错了?任何帮助将不胜感激,谢谢。
编辑:有些人问出了什么问题。嗯,简单地说,正则表达式不起作用。假设我输入一个(有效)MAC 地址,它被标记为无效 MAC 地址。对于以逗号分隔的多个 MAC 地址也是如此。
ASP .NET 页面上的文本框验证器需要正则表达式。如果需要更多详细信息,请告诉我。
I would like to know the correct regex for matching multiple MAC addresses separated my any delimiter, such as a comma.
The regex for a single MAC address would be: ^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$
.
So for multiple MAC addresses delimited by comma, i figured ^(([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2},?)){+}$
would do the trick.
Where am i going wrong? Any help would be appreciated, thanks.
Edit: Some people have asked about what went wrong. Well, simply put, the regex does not work. Let us say i enter a single (valid) MAC address, it is flagged as an invalid MAC address. Same goes for multiple MAC addresses delimited by comma.
The regex is needed for a validator for a textbox on an ASP .NET page. If more details are needed, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}(,([0-9a-fA-F]{ 2}[:-]){5}[0-9a-fA-F]{2})*$
请参阅 逗号分隔列表的正则表达式了解有关创建正则表达式以匹配分隔列表的详细信息。基本上,您需要将 MAC 地址的正则表达式后跟包含逗号的组以及匹配零次或多次的 MAC 地址的正则表达式。在您尝试的解决方案中,逗号是完全可选的。
^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}(,([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2})*$
See Regex for Comma delimited list for details about making a regex to match a delimited list. Basically you need to put the regex for a MAC address followed by a group containing a comma and the regex for a MAC address that is matched zero or more times. In your attempted solution the comma is completely optional.
试试这个 ^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}(,([0-9a-fA-F]{2 }[:-]){5}[0-9a-fA-F]{2})+
Try this ^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}(,([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2})+
这可确保八位字节由
:
或-
分隔。不允许有空格或其他字符。第一组将捕获所有 MAC 地址。匹配将捕获整个字符串,如果无效则无法匹配。This ensures the octets is delimited by
:
or-
. No spaces or other characters are allowed. The first group will capture all the mac addresses. The match will capture the whole string, or fail to match if it isn't valid.