简体   繁体   中英

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

  1. need to mach domain name, foo.com
  2. need to match a.com
  3. 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)
  4. need to match *.a.com or *.foo.com, don't match *foo.com or *_foo.com
  5. 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

  • The pattern does not match _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
  • The pattern does not match *.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM