Regex replacement problem, negative lookahead not behaving as expected
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*)$
"i want to match everything up to the first forward slash, then check that a locale does not exist."
What this (^http://.*?/)(?!en/|\w{2}\-\w{2}/)(?<path>.*?$)
does is match everything up
to the first forward slash that doesent have a en
in front of it.
Its different than matching up to the first forward slash, then fail if en
is in front of it.
The regex will always try to succede taking the shortest path. Even though it acts ungreedy using the ?, it will actually keep going until it satisfies the anchor or condition trailing it. In this case it found a forward slash without en
in front of it: www.mywebsite.com/en/
and that is not the first forward slash, its the second.
This is a gotcha, it happens all the time and is something to note for the future.
So the goal would be to restrain it to match the FIRST forward slash: [^/]*/
Use this regular expression instead:
^(http://[^\/]+/)(?!en/|\w{2}\-\w{2}/)(?<path>.*)$
精彩评论