gpt4 book ai didi

java - 简单字符串的 JAX-RS/Jersey 路径参数正则表达式

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:42:23 24 4
gpt4 key购买 nike

我正在尝试匹配字符串 v1v2。为此,我正在尝试以下正则表达式:^v(1|2)(我也尝试使用 $,这可能是我需要的)。当我在 http://www.regextester.com/ 中测试它时,它似乎工作正常。但是当我在 JAX-RS 路径表达式中使用它时它不起作用。我使用的表达式如下:

@Path("/blah/{ver:^v(1|2)}/ep")

是否有我遗漏的 JAX-RS 特定内容?

最佳答案

由于 anchor ^,您的尝试无效。引用自 JAX-RS specification, chapter 3.7.3 (强调我的):

The function R(A) converts a URI path template annotation A into a regular expression as follows:

  1. URI encode the template, ignoring URI template variable specifications.
  2. Escape any regular expression characters in the URI template, again ignoring URI template variable specifications.
  3. Replace each URI template variable with a capturing group containing the specified regular expression or ‘([ˆ/]+?)’ if no regular expression is specified.
  4. If the resulting string ends with ‘/’ then remove the final character.
  5. Append ‘(/.*)?’ to the result.

因为每个 URI 模板都放在一个捕获组中,所以您不能在其中嵌入 anchor 。

因此,下面的代码将起作用并匹配 v1v2:

@Path("/blah/{ver:v[12]}/ep")

关于java - 简单字符串的 JAX-RS/Jersey 路径参数正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com