【发布时间】:2011-06-06 20:40:52
【问题描述】:
确定文本是否包含不指向特定域的链接的好规则是什么?
我找到了这篇文章,但我需要它的反面: Specific domain URL validation with Regular Expression
【问题讨论】:
确定文本是否包含不指向特定域的链接的好规则是什么?
我找到了这篇文章,但我需要它的反面: Specific domain URL validation with Regular Expression
【问题讨论】:
您可以使用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
}
}
【讨论】: