Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编译Spring的MVC url模式regex_Java_Regex_Spring_Spring Mvc - Fatal编程技术网

Java 编译Spring的MVC url模式regex

Java 编译Spring的MVC url模式regex,java,regex,spring,spring-mvc,Java,Regex,Spring,Spring Mvc,我需要能够将Spring MVC URL模式匹配表达式的列表转换为实际的正则表达式模式,以便能够根据它们匹配字符串 弹簧模式示例: /actionGroups/{action-group-id:\d+} 此时,如果我编译上面的字符串,我会得到一个PatternSyntaxException,因为{字符导致非法重复 Spring是否公开了它用来将这些字符串编译/匹配成实际正则表达式模式的机制?如果我可以重用它们的功能,那就太酷了 否则,我想我需要手动转义{字符并去掉模式名。.本例中的操作组id。

我需要能够将Spring MVC URL模式匹配表达式的列表转换为实际的正则表达式模式,以便能够根据它们匹配字符串

弹簧模式示例:

/actionGroups/{action-group-id:\d+}
此时,如果我编译上面的字符串,我会得到一个PatternSyntaxException,因为{字符导致非法重复

Spring是否公开了它用来将这些字符串编译/匹配成实际正则表达式模式的机制?如果我可以重用它们的功能,那就太酷了


否则,我想我需要手动转义{字符并去掉模式名。.本例中的操作组id。

如果您尝试执行的操作没有问题,请检查您的模式语法:

@RequestMapping("/actionGroups/{action-group-id:\\d+}")
public void test(@PathVariable(value = "action-group-id") Integer id) {
    // you can use id ...
}

根据Sotirios Delimanolis上面的评论,看起来这是我要找的AntPathMatcher类


这个类有一个matches方法,它采用上面的URL模式和一个URL字符串,并根据字符串是否匹配返回true/false。

查看AntPathMatcher的实现。我认为这是默认使用的方法。是否在字符串文本中转义反斜杠,即…\\d+…?是的,这是转义的。