I am trying to match strings v1
and v2
. For that, I am trying the following regex : ^v(1|2)
(I also tried with $
which is probably what I need). When I test it in http://www.regextester.com/ , it seems to work fine. But when I used it in JAX-RS path expression it doesn't work. The expression I use is below:
@Path("/blah/{ver:^v(1|2)}/ep")
Is there anything specific to JAX-RS that I am missing?
Your attempt does not work because of the anchor ^
. Quoting from the JAX-RS specification, chapter 3.7.3 (emphasis mine):
The function
R(A)
converts a URI path template annotationA
into a regular expression as follows:
- URI encode the template, ignoring URI template variable specifications.
- Escape any regular expression characters in the URI template, again ignoring URI template variable specifications.
- Replace each URI template variable with a capturing group containing the specified regular expression or
'([ˆ/]+?)'
if no regular expression is specified.- If the resulting string ends with
'/'
then remove the final character.- Append
'(/.*)?'
to the result.
Because each URI templates is placed inside a capturing group, you can't embed anchors in it.
As such, the following will work and will match v1
or v2
:
@Path("/blah/{ver:v[12]}/ep")
Try the following (without anchors):
@Path("/blah/{ver : v(1|2)}/ep")
Also, if the change is a single character only, use character set instead of the |
operator:
@Path("/blah/{ver : v[12]}/ep")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.