简体   繁体   English

正则表达式和负面展望

[英]Regex and negative look ahead

I am trying to create some regex patterns that match a website domain. 我正在尝试创建一些与网站域匹配的正则表达式模式。

The rules are as below : 规则如下:

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 

I have these Regex patterns for France and Germany which work fine : 我有适用于法国和德国的以下Regex模式,它们工作正常:

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

and

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

However, I am struggling to get a Regex pattern that will match the root domain and other domains (such as www.domain.com/en-us with anything after it) but EXCLUDE /fr-fr.* and /de-de.* 但是,我正在努力获得一个正则表达式模式,该模式将匹配根域和其他域(例如www.domain.com/en-us及其后的任何内容),但EXCLUDE /fr-fr.* and /de-de.*

I have tried a negative lookahead, such as this (for example, NOT france) : 我尝试了负面的前瞻,例如这样(例如,NOT France):

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

But this does not seem to work, and matches against URLs that it should not. 但这似乎不起作用,并且与不应该匹配的URL匹配。

Maybe I am missing something obvious. 也许我缺少明显的东西。

Any help very much appreciated. 任何帮助,非常感谢。

Only "Germany" URLs: 仅“德国”网址:

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

Only "France" URLs: 仅“法国”网址:

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

URLs that are neither "Germany" nor "France" 既不是“德国”也不是“法国”的网址

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM