代码之家  ›  专栏  ›  技术社区  ›  user7693832

如何编写与此正则表达式相反的正则表达式?

  •  0
  • user7693832  · 技术社区  · 7 年前

    (.*\.|)(abc|google)\.(org|net)
    

    我的代理将不会传输 abc.org , abc.net google.org google.net 这里的交通很拥挤。

    abc.org , abc.net , , google.net 这里的交通很拥挤。


    我的想法只是想传播 www.abc.org ,我该怎么办?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Ibrahim    7 年前

    试试这个:

    ^(?!(www\.)?(?:abc|google)\.(?:net|org)).*
    

    演示: https://regex101.com/r/WOnFx8/3/

    ?! 以反转正则表达式的匹配。这样,它将匹配除这4个特定域之外的任何域。

    ^(?!(.*\.|)(?:abc|google)\.(?:net|org)).*
    

    https://regex101.com/r/WOnFx8/4/

        2
  •  1
  •   thang    7 年前

    你写的正则表达式

    比如:test.google.org,sub.abc.net,。。。

    我认为您希望匹配test.yahoo.com之类的字符串,但不希望匹配test.google.org。如果您可以使用“消极展望”,则答案如下:

    ^ (?!

    说明:

    • ^ $ 确保您的匹配是整个url字符串
    • \w+\.\w+ 检查保留的字符串是一种URL类型(如yahoo.com等)
        3
  •  0
  •   Karan Shishoo    7 年前

    我将假设你有lookaheads,如果是这样,那么你可以简单地使用-

    (^.*?\.(?!(abc|google))\w+\.(?:org|net)$)
    

    https://regex101.com/r/5eC41R/3

    1. 查找url的开头(直到第一个url) . )
    2. 检查下一部分是否未被删除 abc google
    3. . )
    4. 寻找一个结束 org net

    请注意,由于它是一个前瞻性的,因此与其他正则表达式匹配相比,它将是缓慢的