regular expression for ipaddress and mac address

前端 未结 7 1518
耶瑟儿~
耶瑟儿~ 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条回答
  •  Happy的楠姐
    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})"
    

提交回复
热议问题