ip地址和mac地址的正则表达式
谁能建议我 ip 地址和 mac 地址的正则表达式?
我正在使用 python & 以Django
为例, http://[ipaddress]/SaveData/127.0.0.1/00-0C-F1-56-98-AD/
for mac 地址我尝试遵循但没有工作
([0-9A-F]{2}[:-]){5}([0-9A-F]{2})
^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$
can anyone suggest me the regular expression for ip address and mac address ?
i am using python & django
for example ,
http://[ipaddress]/SaveData/127.0.0.1/00-0C-F1-56-98-AD/
for mac address i tried following but didn't work
([0-9A-F]{2}[:-]){5}([0-9A-F]{2})
^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将此代码段放入您的 django 路由定义文件中 -
urls.py
Place this snippet in your django routing definitions file -
urls.py
您的正则表达式仅包含两个捕获组(括号),因此它不会存储整个地址(第一组被“覆盖”)。尝试这些:
IP 地址变得更加棘手,因为范围是二进制的,但表示形式是十进制的。
Your regular expression only contains two capturing groups (parentheses), so it isn't storing the entire address (the first group gets "overwritten"). Try these:
IP addresses get trickier because the ranges are binary but the representation is decimal.
您可以使用
/^([0-2]?\d{0,2}\.){3}([0-2]?\d{0,2})$/
IPv4 地址和/^([\da-fA-F]{1,4}:){7}([\da-fA-F]{1,4})$/i
IPv6 地址。您可以将这两者组合为
/^((([0-2]?\d{0,2}\.){3}([0-2]?\d{0,2}))| (([\da-fA-F]{1,4}:){7}([\da-fA-F]{1,4})))$/i
。您可以在此处找到示例。参考:http://snipplr.com/view/49994/ipv4-regex/ , http://snipplr.com/view/49993/ipv6-regex/
对于Mac 地址 您可以使用
/^([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$/i
。您可以在此处找到示例。You can use
/^([0-2]?\d{0,2}\.){3}([0-2]?\d{0,2})$/
for IPv4 Address and/^([\da-fA-F]{1,4}:){7}([\da-fA-F]{1,4})$/i
for IPv6 address.You can combine these two as
/^((([0-2]?\d{0,2}\.){3}([0-2]?\d{0,2}))|(([\da-fA-F]{1,4}:){7}([\da-fA-F]{1,4})))$/i
. You can find a sample here.Ref: http://snipplr.com/view/49994/ipv4-regex/, http://snipplr.com/view/49993/ipv6-regex/
For Mac Address You can use
/^([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$/i
. You can find a sample here.这是 MAC 地址:
This is for MAC address:
考虑 s=256.1.1.1
我想对 Michal 的答案做一些修改:
注意 '(^| )' 表示行开头或前面的空格,以避免从 '256.1.1.1' 获取 '56.1.1.1'
consider s=256.1.1.1
i'd like to make a little modification from Michal's answer:
notice '(^| )' means line start or space ahead, to avoid get '56.1.1.1' from '256.1.1.1'
好的,这就是我用于 IPV4 的内容
([0-9]{1,3}。){3}[0-9]{1,3}
进行测试
使用127.0.0.1
255.255.255.255
适用于所有人
alright so this is what I use for IPV4
([0-9]{1,3}.){3}[0-9]{1,3}
tested with
127.0.0.1
255.255.255.255
and works for all
我需要进行 mac 地址验证,并且必须接受不带分隔符以及带冒号和破折号分隔符的 mac 地址。因此,像这样的有效格式
和混合分隔符是无效的,像这样
和 带有正则表达式的验证代码就像这样。
I need to mac address validation and I have to accept mac address without separator and with colon and dash separators. So valid formats like this
and mixed separators are invalid like this
and the validation code with regex like this.