4
How to create a regular expression to validate an internet domain? The rules are below:
- Minimum size 2 and maximum 26 characters;
- Valid characters are letters from "a" to "z", numbers from "0" to "9" and the hyphen;
- Not only contain numbers;
- Do not start or end by hyphenating.
Remember that the validation in question refers between the beginning of the value and the first point. Example: domain.with.br.
Will be used for both URL and email.
Follow the code I made:
([^-](([a-zA-Z0-9]?)*([a-zA-Z-])([a-zA-Z0-9]?))+[^-])\.
As close as I got to the answer, the hyphens need to be removed at the beginning and end:
((([\w]?)+([a-zA-Z-_])([\w]?)+){2,26})\.
These rules may have problems depending on where to apply. They serve well on a DNS entry, but if it’s for user interaction, they’re wrong. Accented characters and other languages are valid in the data input. Conversion to punycode is not the responsibility of the user, but of the system. Another thing, it can not have two points in a row (and two hyphens then usually only at the beginning of the punycode). Example of a link that will be blocked: http://www.estadão.com.br
– Bacco
You’re right, @Bob. Accents can (and should) be applied, but I’m not as sure about the point or hyphen followed (programmatically speaking).
– Marcos Lopes
As close as I got to the answer, the hyphens must be removed at the beginning and end:
((([\w]?)+([a-zA-Z-_])([\w]?)+){2,26})\.
– Marcos Lopes