Validate a hostname string

后端 未结 10 1529
生来不讨喜
生来不讨喜 2020-12-04 13:24

Following up to Regular expression to match hostname or IP Address? and using Restrictions on valid host names as a reference, what is the most readable, concise way to matc

10条回答
  •  庸人自扰
    2020-12-04 14:00

    Process each DNS label individually by excluding invalid characters and ensuring nonzero length.

    
    def isValidHostname(hostname):
        disallowed = re.compile("[^a-zA-Z\d\-]")
        return all(map(lambda x: len(x) and not disallowed.search(x), hostname.split(".")))
    

提交回复
热议问题