Regex and negative look ahead

324 views Asked by At

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.

1

There are 1 answers

1
Tomalak On BEST ANSWER

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