Regex does not work with Stream () filter

I am trying to get specific text from a string that I get when using new Streamin Java 8.

This is what I read:

46 [core1]
56 [core1]
45 [core1]
45 [core2]
67 [core2]
54 [core2]

And here is the code that I have read at the moment:

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());
            }

I checked the regex expression at https://www.regex101.com/ and it seems to work fine. Also, if I'm just looking for a string [core1], it will find it.

The problem is that when using all this together, I get 0 matches. My logic in this is that I read the line, see what kind of core it is, and then get it to it. After that I want to add it to the list.

What am I doing wrong here?

+4
1

contains ( )... line.matches("(\\d+).*") .

+9

Source: https://habr.com/ru/post/1584100/


All Articles