Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match trailing slash except pattern

Tags:

regex

I'm looking for strings that end with / but not when the string is equal to /[a-z]{2}/ (with the 2 slashes in the pattern)

To exclude the unwanted string I would use :

(?!/[a-z]{2}/)

For strings ending in a slash I'd use :

.*/$

However, my limited knowledge with regular expression doesn't allow me to combine the two patterns. How would I do this ?

This would match :

/en/contact/

This wouldn't :

/en/
like image 570
Sparkup Avatar asked May 01 '26 06:05

Sparkup


1 Answers

The regex should work for you:

^(?!\/[a-z]{2}\/).*\/$

You could check it here.

Update for your added requirement:

^(?!^\/[a-z]{2}\/$).*\/$

The demo.

like image 142
xdazz Avatar answered May 03 '26 07:05

xdazz