I'm trying to match some URLs against another table using regex and - because the original source wasn't put together properly, I'm using a regex to clean them within the SQL.
As an example, the URLs might be /this-is-my-test-string/
or /this-is-my-test-string
and the reference table is always of the form /this-is-my-test-string
so using this regex works well to capture the matching part.
(\/[^\/)]*)\/?
However I've now come across some others with the form /this-is-my-test-string-
and /this-is-my-test-string-/
which aren't as straightforward - I can't just add -
to the exclusion as it's present in the rest of the string. From reading around - regex is not something I use regularly - a lookahead would seem to be the answer, but I can't work out how to include this in the expression.
Any help would be gratefully received.
CodePudding user response:
You can use $
to anchor the end of the string, and use a non-greedy quantifier *?
on the non-slash character set to allow -?
to match a -
from (or near) the end of the string:
(\/[^\/)]*?)-?\/?$