代码之家  ›  专栏  ›  技术社区  ›  Chris Marks

正则表达式和负展望

  •  0
  • Chris Marks  · 技术社区  · 7 年前

    我正在尝试创建一些与网站域匹配的正则表达式模式。

    For France, the URL pattern must have /fr-fr (followed by anything else) after the domain name, ie www.domain.com/fr-fr/anything
    For Germany, the URL pattern must have /de-de (followed by anything else) after the domain name, ie www.domain.com/de-de/anything
    And for all other countries, the URL pattern can be the root domain (ie www.domain.com) OR anything EXCEPT fr-fr and de-de after the domain name 
    

    我有这些适用于法国和德国的正则表达式模式,效果很好:

    https?://www.domain.com.*?/(?i)FR-FR.\*
    

    https?://www.domain.com.*?/(?i)DE-DE.\*
    

    然而,我正在努力获得一个正则表达式模式,该模式将匹配根域和其他域(例如www.domain.com/en-us和它后面的任何内容),但是 EXCLUDE /fr-fr.* and /de-de.*

    我尝试过负面展望,例如(例如,不是法国):

    https?://www.domain.com.*?/(?!fr-fr).\*
    

    但这似乎不起作用,并且与不应该的URL相匹配。

    也许我错过了一些明显的东西。

    非常感谢您的帮助。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Tomalak    7 年前

    仅“德国”URL:

    ^(?i)https?://www.domain.com(:\d+)?/de-de(/.*)?$
    

    仅“法国”URL:

    ^(?i)https?://www.domain.com(:\d+)?/fr-fr(/.*)?$
    

    既不是“德国”也不是“法国”的URL

    ^(?i)https?://www.domain.com(:\d+)?(?!/fr-fr|/de-de)(/.*)?$