确定文本是否包含不指向特定域的链接有什么好处?
我找到了这篇文章,但我需要与之相反: Specific domain URL validation with Regular Expression
答案 0 :(得分:1)
您可以使用answer your linked中的确切代码,但将您的逻辑置于“不匹配”的情况下。这是我的快速.Net重写:
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
}
}