Kas*_*par 5 java regex lambda java-8 java-stream
我试图从StreamJava 8中使用new时获得的一行中获取某些文本.
这就是我在读的内容:
46 [core1]
56 [core1]
45 [core1]
45 [core2]
67 [core2]
54 [core2]
Run Code Online (Sandbox Code Playgroud)
这是我目前阅读的代码:
Path path = Paths.get("./src/main/resources/", "data.txt");
try(Stream<String> lines = Files.lines(path)){
List<Integer> temps = new ArrayList<>();
lines
.filter(line -> line.contains("[core1]"))
.filter(line -> line.contains("(\\d+).*"))
.flatMapToInt(temperature -> IntStream.of(Integer.parseInt(temperature)))
.forEach(System.out::println);
System.out.print(temps.size());
}
Run Code Online (Sandbox Code Playgroud)
我已经检查了https://www.regex101.com/中的正则表达式,它似乎工作正常.此外,如果我只是搜索[core1]字符串,它会找到它.
问题是,当一起使用所有这些时,我得到0匹配.我当前的逻辑是我读了一行,看看它是什么核心,然后在它之前得到它的数字.之后我想将它添加到List中.
我在这做错了什么?
kar*_*ala 11
contains仅适用于字符串(不支持正则表达式)...您可以使用它line.matches("(\\d+).*")来实现相同的功能.