I am trying to split this type string using a Java Regex:
/api/vX/client/domain/category/id
crudely into this:
(?:/api)?(?:/vX)?(/client/domain/...)?(?:/category)?(?:...)?
I would split it into the following groups:
- [0] /api
- [1] /vX (1-x)
- [2] /client/domain (/a/b/...) (up to category below)
- [3] /category1 | /category2
- [4] /everything else
Right now, I am trying to us a regex like this but it is just not working the way I am expecting it to.
(\/api)?(\/v\d+)?(\/\w+)(\/category1|category2\/?.*)?
I also need to take into account trailing/leading slashes with the expectation a leading slash will always start a segment but a trailing slash may or may not be there (unless there is a next segment).
Some example of paths and outputs I am trying to achieve are:
/client:
[0], [1], [2]=/client, [3], [4]
/api/client:
[0]=/api, [1], [2]=/client, [3], [4]
/api/v1/client/domain:
[0]=/api, [1]=/v1, [2]=/client, [3], [4]
/api/v1/client/domain/category1:
[0]=/api, [1]=/v1, [2]=/client/domain, [3]=/category1, [4]
api/v1/client/d1/d2/d3/category1:
[0]=/api, [1]=/v1, [2]=/client/d1/d2/d3, [3]=/category1, [4]
/api/v2/client/domain/category2/id:
[0]=/api, [1]=/v2, [2]=/client/domain, [3]=/category2, [4]=/id