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
}
}
精彩评论