just trying to work out the regex for this. Say I have to following list of URL paths
/v1/users/
/v1/users/123abc/
/v1/users/123abc/456def/
/v1/users/123abc/456def/789ghi/
/v1/users/123abc/me/
/v1/users/123abc/me/456def/
where some parts are set, like v1 and users, and some parts are path parameters so they can be any values/characters, like 123abc and 456def.
What regex pattern can I put in place for the path parameters so it matches against the right ones.
For example, I tried to get /v1/users/123abc/456def/ using ^/v1/users/.*?/.*?/$. However, this regex matched with the following:
/v1/users/123abc/456def/
/v1/users/123abc/456def/789ghi/
/v1/users/123abc/me/
/v1/users/123abc/me/456def/
I understand it may be impossible to not match with /v1/users/123abc/me/ however I have a way around this if someone can find a solution which can get both /v1/users/123abc/456def/ and /v1/users/123abc/me/.
Thanks in advance.
CodePudding user response:
You can replace both .*? with [^/]* or even [^/] (as subparts must contain at least one char) and use
^/v1/users/[^/] /[^/] /$
See the regex demo.
Details:
^- start of string/v1/users/- a literal string[^/]- one or more chars other than//- a/char[^/]- one or more chars other than//- a/char$- end of string.
