Regular Expression for validating DNS label ( host name)

后端 未结 6 1303
长发绾君心
长发绾君心 2020-12-03 21:11

I would like to validate a hostname using only regualr expression.

Host Names (or \'labels\' in DNS jargon) were traditionally defined by RFC 952 and RFC 1123 and m

6条回答
  •  一生所求
    2020-12-03 21:32

    ^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?

    I used the following testbed written in Python to verify that it works correctly:

    tests = [
        ('01010', False),
        ('abc', True),
        ('A0c', True),
        ('A0c-', False),
        ('-A0c', False),
        ('A-0c', True),
        ('o123456701234567012345670123456701234567012345670123456701234567', False),
        ('o12345670123456701234567012345670123456701234567012345670123456', True),
        ('', True),
        ('a', True),
        ('0--0', True),
    ]
    
    import re
    regex = re.compile('^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?

提交回复
热议问题