'A complex domain name match rewrite rule
I am trying to create a regex rule for the following,
The rule is for matching domain names with the following conditions
- need to mach domain name , foo.com
- need to match a.com
- need to match domain name start with underscrore and followed by a charector number ( _f.a.com, _foo.foo.com , don't match _.com or _*.a.com)
- need to match *.a.com or *.foo.com , don't match *foo.com or *_foo.com
- don't end with dot
So far I reached the following regex
^(([a-zA-Z0-9]|\*\.[a-zA-Z0-9])|[a-zA-Z0-9_][a-zA-Z0-9-]){0,254}[a-zA-Z0-9](?:\.[a-zA-Z0-9]{2,})+$
The problem is it doesn't match *.a.com , it will match all other rule
Any help is appreciated
Solution 1:[1]
- The pattern does not match
_f.a.combecause the_and thefcan be matched here[a-zA-Z0-9_][a-zA-Z0-9-], but the following character is a dot that can not be matched by any of the alternatives and also not by the[a-zA-Z0-9]part right after the alternation - The pattern does not match
*.a.comfor almost the same reason, only this time for*.
You can match all the examples using:
^(?:_|\*\.)?[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$
Explanation
^Start of string(?:_|\*\.)?Optionally match either_or.*[a-zA-Z0-9]+Match 1+ times any of the ranges(?:\.[a-zA-Z0-9]+)*Optionally repeat the previous with a leading dot$End of string
See a regex demo.
You can make the pattern more specific if you want, for example if there should be 2 or more characters after the last dot:
^(?:_|\*\.)?[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*\.[a-zA-Z0-9]{2,}$
You can use the quantifier {0,254} for any of the character classes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The fourth bird |
