regular expression for ipaddress and mac address

前端 未结 7 1465
耶瑟儿~
耶瑟儿~ 2021-01-06 05:59

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.

相关标签:
7条回答
  • 2021-01-06 06:43

    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.

    0 讨论(0)
  • 2021-01-06 06:44

    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})"
    
    0 讨论(0)
  • 2021-01-06 06:44

    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
    
    0 讨论(0)
  • 2021-01-06 06:47

    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

    0 讨论(0)
  • 2021-01-06 06:48
    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()),
    
    0 讨论(0)
  • 2021-01-06 06:51

    This is for MAC address:

    ([0-9A-F]{2}[:-]){5}([0-9A-F]{2})
    
    0 讨论(0)
提交回复
热议问题