You could combine dropWhile
and takeWhile
methods introduced in Java 9. If you are searching for lets say [Category 2]
drop all lines before it, and continue processing all lines until you find empty line (or until stream will end but that case is covered automatically).
Demo (for more info about split("\\R")
see https://stackoverflow.com/a/31060125):
String data =
"[Category 1]\n" +
"1 2 3 4\n" +
"5 6 7 8\n" +
"\n" +
"[Category 2]\n" +
"9 8 7 6\n" +
"5 4 3 2\n" +
"\n" +
"[Category 3]\n" +
"1 3 7 6\n" +
"4 4 3 2\n";
Stream.of(data.split("\\R")) // form Java 8 regex engine supports \R as line separator
.dropWhile(line->!line.equals("[Category 2]"))
.takeWhile(Predicate.not(String::isEmpty))
.forEach(System.out::println);
Output:
[Category 2]
9 8 7 6
5 4 3 2