1
In a domain validation task, I need a regular expression to validate them by following some rules, among them, where the domain name does not only contain numbers.
Therefore, I created a regular expression (regex) that validates the domain, but I could not make it fail in case of only numbers in the domain name.
In short, the regular expression is failing by allowing domains that contain only numbers, as they are invalid domains and the expression is allowing the same.
Regular expression:
^(^[a-z][a-z0-9]{0,30}\.)?([a-z0-9](?:[a-z0-9-]{0,24}[a-z0-9])?)(\.[a-z]{2,4}(?:\.[a-z]{2})?)$
In the expression above, I’m capturing three groups:
- Group 1: subdomain;
- Group 2: domain name;
- Group 3: TLD.
I’m trying to apply a Negative Lookahead only in group 2, but example I found applies only to whole result.
I am trying to apply only in group 2 (which captures the domain), but without success.
I made an example in Regexr to demonstrate better what I’m trying to do.
Thank you very much! It worked perfectly! I think I now understand the operation of the Lookahead Negative! Thank you very much indeed!
– Allan Andrade