小编use*_*838的帖子

对Pattern.matcher和Predicate.test的不同响应

该代码的解释是什么?

   String key = "camera.cameraConfiguration.2a";
   // This regex just allows "extensions", anything after the \d+ block, to the usual format
   String regex = "camera.cameraConfiguration\\.\\d+(\\..*)?";
   Predicate<String> predicate = Pattern.compile(regex).asPredicate();

   // This one used predicate --> Return true BUT it should be FALSE because the regex says: give me only   number then an optional string formatted like ".thenAnyCharYouWant"
   System.out.println(predicate.test(key));
   // Return false
   System.out.println(key.matches(regex));
   // Return false
   System.out.println(Pattern.compile(regex).matcher(key).matches());
Run Code Online (Sandbox Code Playgroud)

为什么Predicate.test似乎给出错误的答案?

java regex

-1
推荐指数
1
解决办法
34
查看次数

标签 统计

java ×1

regex ×1