【问题标题】:Regular Expression to Find Links that Do NOT Point to a Specific Domain正则表达式查找不指向特定域的链接
【发布时间】:2011-06-06 20:40:52
【问题描述】:

确定文本是否包含不指向特定域的链接的好规则是什么?

我找到了这篇文章,但我需要它的反面: Specific domain URL validation with Regular Expression

【问题讨论】:

    标签: .net regex dns


    【解决方案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
      }
    }
    

    【讨论】:

    • 我认为它关闭了,但我必须从字符串中提取所有链接,然后检查是否有任何不指向特定域的链接。
    • 你能用你想要处理的字符串的例子来更新你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2016-08-09
    相关资源
    最近更新 更多