Regular expression formatted as internet domain

4

How to create a regular expression to validate an internet domain? The rules are below:

  • Minimum length of 2 and a maximum of 26 characters;
  • Valid characters are letters from "a" to "z", numbers from "0" to "9" and the hyphen;
  • Do not only contain numbers;
  • Do not start or end with a hyphen.
  • Remember that the validation in question refers between the beginning of the value and the first point. Example: domain .com.br.

    It will be used for both URLs and emails.

    Here is the code I did:

    ([^-](([a-zA-Z0-9]?)*([a-zA-Z-])([a-zA-Z0-9]?))+[^-])\.
    

    The closest I got to the answer, the hyphens need to be removed at the beginning and end:

    ((([\w]?)+([a-zA-Z-_])([\w]?)+){2,26})\.
    
        
    asked by anonymous 22.01.2016 / 20:19

    1 answer

    1

    What about:

    ^(?!\d+\.)(\w[\w\-]{0,24}\w)\.
    
    • (?!\d+\.) is a negative lookahead. He checks that the domain is not just digits.

    • \w after the first character is consistently a [\w\-]{0,24} , there may be 0 or 24 letters, numbers, or hyphen.

    • não hífen The last character can not also be a hyphen.

    • \w start of the rest of the domain you are not interested in.

    In \. you expect (\w[\w\-]{0,24}\w) + 1 caracter não hífen + 0 até 24 letras, números ou hífen . These three rules together guarantee that your domain will be at least 2 characters and a maximum of 26 characters.

    You can see the regex running here .

    Notes:

    The above regex does not handle letters in other languages, as commented out by @Bacoo, because ECMAScript 5 and below do not provide native support for Unicode regexes ( ECMAScript 6 sim ).

    The same regex could be implemented in PHP (which supports Unicode regexes) as follows:

    ^(?!\d+\.)([\p{L}0-9][\p{L}0-9\-]{0,24}[\p{L}0-9])\.
    

    where 1 caracter não hífen replaces [\p{L}0-9] to accept any letters in any languages + digits. You can see this regex running here .

        
    27.02.2016 / 05:44