简体   繁体   中英

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 :

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.*

I have tried a negative lookahead, such as this (for example, NOT france) :

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

But this does not seem to work, and matches against URLs that it should not.

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)(/.*)?$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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