regular expression for ipaddress and mac address

前端 未结 7 1532
耶瑟儿~
耶瑟儿~ 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: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
    

提交回复
热议问题