【问题标题】:Negate Word Groups? - Regular Expression (Regex)否定词组? - 正则表达式(正则表达式)
【发布时间】:2015-06-16 21:32:51
【问题描述】:

很容易获得一个正则表达式,让您获得所有“com/net/org”

这里是:

^.+(com|org|net)$

但是,我正在尝试查找所有没有 com/net/org 的域

我似乎找不到否定词组 com/net/org 的方法

这里是示例字符串:

piquetraveldesign.com
rigall.com.au
hunt4me.co.uk
cialis-without-prescription.us
filipinocommunityseattle.org
mortgageplanningblog.com
mental-health-training.net
final-fantasy-xiv-gil.com
uoblife.com.sg
unashamedandassociates.biz
heaonlineshop.com

有什么想法吗?

【问题讨论】:

    标签: regex regex-negation


    【解决方案1】:

    您可以使用 Negative Lookahead 断言。

    ^(?!.*(?:com|net|org)$).+$
    

    【讨论】:

    • 效果惊人。只是好奇为什么最后没有 .+ 就不行?
    • 因为(?!...) 是一个仅在给定位置匹配的零宽度断言。
    【解决方案2】:

    由于其长度固定,您可以使用 否定 后向断言。
    如果所有行都在一个字符串中,则必须设置多行模式
    所以$ 表示行尾,而不是字符串的结尾。

     # .+(?<!\.(?:com|org|net))$
    
     .+ 
     (?<!
          \.
          (?: com | org | net )
     )
     $
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 2011-03-28
      相关资源
      最近更新 更多