Search code examples
pythonregexdnssubdomain

Regex for sub domain


I have a system that can generate one-level subdomains, I want a regex expression to validate the subdomain before creating it.

So far I have come up with:

^[a-zA-Z0-9]+[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]*$

from my test cases it

  • Restricts use of hyphen at start and end
  • allows only letters, numbers, and hyphens
  • limits the total length to 63

I still am not sure if my pattern is correct. If someone can give me a better regex or point out any rule that I forgot about, please let me know.


Solution

  • There are a couple of problems with your pattern:

    1. Using the + quantifier after the first character class allows for infinitely long strings (the {0,61} quantifier becomes redundant).

    2. Using the * quantifier after the last character class allows it to match zero chars (which makes the pattern accept a hyphen at the end) and also has the same problem as above.

    You may use:

    ^(?=[a-zA-Z0-9].{0,62}$)[a-zA-Z0-9\-]*[a-zA-Z0-9]$
    

    Demo.

    Breakdown:

    • ^ - Beginning of the string.
    • (?= - Start of a positive Lookahead.
      • [a-zA-Z0-9] - The first character must be alphanumeric only.
      • .{0,62} - Followed by up to 62 characters.
      • $ - Then, the end of the string.
    • ) - End of the Lookahead.
    • [a-zA-Z0-9\-]* - Match 0 or more alphanumeric characters or hyphens.
    • [a-zA-Z0-9]$ - Match one alphanumeric character at the end of the string.

    If you need to set a minimum length, just change {0,62} to {n,62} where n+1 is the minimum length. Or you could get rid of the Lookahead (as long as the minimum length is >= 2) and use:

    ^[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]$
    

    ...replacing {0,61} with {n,61} where n+2 is the minimum length.