Simple regex for domain names

℡╲_俬逩灬. 提交于 2019-12-11 01:15:31

问题


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 criteria :

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

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

Thanks for your help.


回答1:


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!




回答2:


"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




回答3:


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.




回答4:


^[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)



回答5:


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';
  }
}


来源:https://stackoverflow.com/questions/7742010/simple-regex-for-domain-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!