Adam Devin
Adam Devin

Reputation: 63

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

What's a good regular 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

Upvotes: 1

Views: 122

Answers (1)

Dexter
Dexter

Reputation: 18452

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

Upvotes: 1

Related Questions