I am trying to create a regex rule for the following,
The rule is for matching domain names with the following conditions
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
_f.a.com
because the _
and the f
can 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*.a.com
for 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.