Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

regex - Can't map regular expression - java.lang.IllegalArgumentException: The number of capturing groups in the pattern segment

I have the following method defined in my controller:

@RequestMapping(value = "/ajax/comments/post/{contentId:([apv]|ad)\d+}")
    public @ResponseBody
    ActionResult handlePostCommentRequest(HttpServletRequest request, Model model,
            @PathVariable("contentId") String assetId,
            @RequestParam(value = "nickName", required = false, defaultValue = "Anonyymi") String nickName,
            @RequestParam(value = "text", required = false, defaultValue = "") String text,
            @RequestParam(value = "createThread", required = false, defaultValue = "false") String createThread) {
            // some code...
}

However, when I do the following HTTP request - /ajax/comments/post/ad1374659405664 I get exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: The number of capturing groups in the pattern segment (([apv]|ad)d+) does not match the number of URI template variables it defines, which can occur if capturing groups are used in a URI template regex. Use non-capturing groups instead.

Google doesn't give that much results and it is weird, because when I check the regex ([vpa]|ad)d+ in http://regexpal.com/ it matches everything correctly. What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Everything is said in the error message: use non-capturing groups instead

(?:[apv]|ad)\d+

See http://www.regular-expressions.info/brackets.html for further details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...