开发者

Simple regex for domain names

开发者 https://www.devze.com 2023-04-12 18:24 出处:网络
How to make sure that the domain name match those 3 simple criterias : Ends with .com / .net Must not start with

How to make sure that the domain name match those 3 simple criterias :

  • Ends with .com / .net

Must not start with

  • http:// or https://
  • http://www. or https://www.

I've managed to understand this part of the regex which correspond with the first crit开发者_如何学运维eria :

/.*(\.com|\.net)$/

But i have no idea how to achieve the 2 others conditions to make an unique regex.

Thanks for your help.


"Not starting" with a pattern is a bit tricky.

The clearest way of doing it is two separate regexes, one to match what you want and one not matching what you don't want.

But you can do this in one with a negative look-ahead:

/^(?!https?:\/\/(www\.)?).*(\.com|\.net)$/

Edit: correct the assertion as pointed out by ridgerunner


A regex solution is easy. Simply assert a negative lookahead at the start of the string like so: (With comments...)

if (preg_match('%
    # Match non-http ,com or .net domain.
    ^             # Anchor to start of string.
    (?!           # Assert that this URL is NOT...
      https?://   # HTTP or HTTPS scheme with
      (?:www\.)?  # optional www. subdomain.
    )             # End negative lookahead.
    .*            # Match up to TLD.
    \.            # Last literal dot before TLD.
    (?:           # Group for TLD alternatives.
      net         # Either .net
    | com         # or .com.
    )             # End group of TLD alts.
    $             # Anchor to end of string.
    %xi', $text)) {
    // It matches.
} else {
    // It doesn't match.
}

Note that since: http://www. is a subset of: http://, the expression for the optional www. is not necessary. Here is a shorter version:

if (preg_match('%^(?!https?://).*\.(?:net|com)$%i', $text)) {
    // It matches.
} else {
    // It doesn't match.
}

Simple regex to the rescue!


If you need to be sure that a string will not contain the first two points, why don't you simply use str_replace and then test for the first criteria? I think it will be more easy and surely more efficient.


^[a-zA-Z\.]+\.(com|net)$

does this work?

well if I understood you right, you want to check a list of String, and find out which are domain names. e.g.

http://www.a.b (F)
a.com (T)
b.net  (T)
https://google.com (F)


Try this:

if(preg_match('/^(?:http://|https://)(?:[w]{3}|)/i', $subject))
{
  echo 'Fail';
}
else
{
  if(preg_match('/(?:.*(\.com|\.net))$/i', $subject))
  {
    echo 'Pass';
  }
  else
  {
    echo 'Fail';
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号