开发者

Regular Expression to Find Links that Do NOT Point to a Specific Domain

开发者 https://www.devze.com 2023-03-10 15:45 出处:网络
What\'s a good regu开发者_运维技巧lar to determine if text contains links that DO NOT point to a specific domain?

What's a good regu开发者_运维技巧lar to determine if text contains links that DO NOT point to a specific domain?

I found this post, but I need the opposite of it: Specific domain URL validation with Regular Expression


You can use the exact code in the answer your linked, but put your logic into the "did not match" case. Here's my quick .Net re-write:

Regex r = new Regex(@"^https?://([a-z0-9-]+\.)*blah\.com(/.*)?$";

string[] tests = {
    'http://blah.com/so/this/is/good'
  , 'http://blah.com/so/this/is/good/index.html'
  , 'http://www.blah.com/so/this/is/good/mice.html#anchortag'
  , 'http://anysubdomain.blah.com/so/this/is/good/wow.php'
  , 'http://anysubdomain.blah.com/so/this/is/good/wow.php?search=doozy'
  , 'http://any.sub-domain.blah.com/so/this/is/good/wow.php?search=doozy' 
  , 'http://999.sub-domain.blah.com/so/this/is/good/wow.php?search=doozy' 
  , 'http://obviousexample.com'
  , 'http://bbc.co.uk/blah.com/whatever/you/get/the/idea'
  , 'http://blah.com.example'
  , 'not/even/a/blah.com/url'
}

foreach (string url in tests ) {
  if ( !r.Matches(url) )
  {
    // Did not match
  }
}
0

精彩评论

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