I’m trying to write some replacement regex that will insert a locale code into a url if it doesn’t already exist. I’m usig the negative lookahead pattern to achieve this shown below
(^http://.*?/)(?!en/|\w{2}\-\w{2}/)(?<path>.*?$)
So i want to match everything up to the first forward slash, then check that a locale does not exist. Locales can be either ‘en’ or the usual ‘en-GB’ style locale code in our site. Currently this pattern will do the following:
http://www.mywebsite.com/location/index.html => http://www.mywebsite.com/en/location/index.html
http://www.mywebsite.com/en/location/index.html => http://www.mywebsite.com/en/en/location/index.html
using the following replacement pattern: $1en/${path}
So the first one works correctly, but the second one matches even though i don’t want it to and then puts the locale code in anyway.
Is what i want to do possible, it sounds like it should be. Thanks for any help in advance.
Try replacing the first
.*?with[^/]*.For example:
^(http://[^/\s]*/)(?!en/|\w{2}-\w{2}/)(?<path>\S*)$