[英]Why this regex pattern matching does not work
I am implementing RESTful query with JPA specification. 我正在使用JPA规范实现RESTful查询。 Would like to handle url with multiple criteria that looks like this: 想要使用多个条件来处理url,如下所示:
http://localhost:8080/samples?search=lastName:doe,age>25
The search string would match pattern (\\\\w+?)(:|<|>)(\\\\w+?)
separated by ",". 搜索字符串将匹配模式(\\\\w+?)(:|<|>)(\\\\w+?)
中间用“,”分隔。
Therefore, I wrote the following code to obtain Matcher from a string: 因此,我编写了以下代码以从字符串获取Matcher:
static Matcher getMatcherFromString(String str) {
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
Matcher matcher = pattern.matcher(str + ",");
return matcher;
}
This method is then called in controller to parse the url. 然后在控制器中调用此方法以解析url。
However, when I tested the method with string analysisId:fdebfd6e-d046-4192-8b97-ac9f65dc2009
, it returns null. 但是,当我使用字符串analysisId:fdebfd6e-d046-4192-8b97-ac9f65dc2009
测试该方法时,它返回null。 Why did I do wrong for the pattern matching? 为什么我在模式匹配上做错了?
I got this to work: (\\w+?)(:|<|>)[a-zA-Z0-9\\-]*, 我使它工作:(\\ w +?)(:| <|>)[a-zA-Z0-9 \\-] *,
I used this to figure it out: https://regex101.com/r/fOEzd9/1 我用它来弄清楚: https : //regex101.com/r/fOEzd9/1
I think the main problem was the numbers makes \\w not match as a word. 我认为主要的问题是数字使\\ w字词不匹配。 You also need to account for the dashes. 您还需要考虑破折号。
我遇到了同样的问题,并使用以下字符串解决了问题:
"(\\w+?)(:|<|>)(\\w+?),"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.