ip地址和mac地址的正则表达式

发布于 2024-10-21 12:49:47 字数 264 浏览 3 评论 0原文

谁能建议我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

阳光下的泡沫是彩色的 2024-10-28 12:49:47
import re
s = "http://[ipaddress]/SaveData/127.0.0.1/00-0C-F1-56-98-AD/"

re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', s, re.I).group()
'00-0C-F1-56-98-AD'

re.search(r'((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9])', s, re.I).group()
'127.0.0.1'

将此代码段放入您的 django 路由定义文件中 - urls.py

url(r'^SaveData/(?P<ip>((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))/(?P<mac>([0-9A-F]{2}[:-]){5}([0-9A-F]{2}))', SaveDataHandler.as_view()),
import re
s = "http://[ipaddress]/SaveData/127.0.0.1/00-0C-F1-56-98-AD/"

re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', s, re.I).group()
'00-0C-F1-56-98-AD'

re.search(r'((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9])', s, re.I).group()
'127.0.0.1'

Place this snippet in your django routing definitions file - urls.py

url(r'^SaveData/(?P<ip>((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))/(?P<mac>([0-9A-F]{2}[:-]){5}([0-9A-F]{2}))', SaveDataHandler.as_view()),
一抹苦笑 2024-10-28 12:49:47

您的正则表达式仅包含两个捕获组(括号),因此它不会存储整个地址(第一组被“覆盖”)。尝试这些:

# store each octet into its own group
r"([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})"
# store entire MAC address into a single group
r"([\dA-F]{2}(?:[-:][\dA-F]{2}){5})"

IP 地址变得更加棘手,因为范围是二进制的,但表示形式是十进制的。

# store each octet into its own group
r"(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))"
# store entire IP address into a single group
r"((?:\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))(?:\.(?:\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))){3})"

Your regular expression only contains two capturing groups (parentheses), so it isn't storing the entire address (the first group gets "overwritten"). Try these:

# store each octet into its own group
r"([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})[-:]([\dA-F]{2})"
# store entire MAC address into a single group
r"([\dA-F]{2}(?:[-:][\dA-F]{2}){5})"

IP addresses get trickier because the ranges are binary but the representation is decimal.

# store each octet into its own group
r"(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))"
# store entire IP address into a single group
r"((?:\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))(?:\.(?:\d|[1-9]\d|1\d\d|2(?:[0-4]\d|5[0-5]))){3})"
情愿 2024-10-28 12:49:47

您可以使用 /^([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.

输什么也不输骨气 2024-10-28 12:49:47

这是 MAC 地址:

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

This is for MAC address:

([0-9A-F]{2}[:-]){5}([0-9A-F]{2})
傾旎 2024-10-28 12:49:47

考虑 s=256.1.1.1
我想对 Michal 的答案做一些修改:

def find_ip(s):
    part = '(2[0-4]|1[0-9]|[0-9])?[0-9]|25[0-5]'
    res =re.search(r'(^| )((%s)\.){3}(%s)' %(part,part), s,re.I )
    if res:
        return res.group().strip()
    else:
        return ''

注意 '(^| )' 表示行开头或前面的空格,以避免从 '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:

def find_ip(s):
    part = '(2[0-4]|1[0-9]|[0-9])?[0-9]|25[0-5]'
    res =re.search(r'(^| )((%s)\.){3}(%s)' %(part,part), s,re.I )
    if res:
        return res.group().strip()
    else:
        return ''

notice '(^| )' means line start or space ahead, to avoid get '56.1.1.1' from '256.1.1.1'

时光与爱终年不遇 2024-10-28 12:49:47

好的,这就是我用于 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

染年凉城似染瑾 2024-10-28 12:49:47

我需要进行 mac 地址验证,并且必须接受不带分隔符以及带冒号和破折号分隔符的 mac 地址。因此,像这样的有效格式

  • aa:bb:cc:dd:ee:ff
  • aa-bb-cc-dd-ee-ff
  • aabbccddeeff

和混合分隔符是无效的,像这样

  • aa:bb-cc-dd:ee:ff

带有正则表达式的验证代码就像这样。

def validate_mac_address(mac_addr):
    pattern = '^(([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})|([0-9a-fA-F]{2}[-]){5}([0-9a-fA-F]{2})|[0-9a-fA-F]{12})

    return not re.match(pattern, mac_addr) is None

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

  • aa:bb:cc:dd:ee:ff
  • aa-bb-cc-dd-ee-ff
  • aabbccddeeff

and mixed separators are invalid like this

  • aa:bb-cc-dd:ee:ff

and the validation code with regex like this.

def validate_mac_address(mac_addr):
    pattern = '^(([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})|([0-9a-fA-F]{2}[-]){5}([0-9a-fA-F]{2})|[0-9a-fA-F]{12})

    return not re.match(pattern, mac_addr) is None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文