You may simply use a String#split method(here is a javadoc for it) with the new line sign \\n to split the text into the lines here, iterating over resulting array of the string representation of the lines, collecting them, for example, in some object representation, like some list of Title objects with it's subtitles. Creating a new title after every empty line. Somethin like this:
String[] lines = str.split("\\n");
List<List<String>> titles = new LinkedList<>();
List<String> title = new LinkedList<>();
titles.add(title);
for (String line : lines) {
if (line.trim().isEmpty()) {
title = new LinkedList<>();
titles.add(title);
continue;
}
title.add(line);
}
System.out.println(titles.get(1));
If that doesn't pass to you, you have to take a look at the (?:) regex operator, which is non-capturing group. You may use it in some pattern to determine, that your Title 2 goes just after an empty line (this empty line use to be in non-capturing group). And then just specify your groups, which you want to be recieved from your text.
jump to Title 2? IsTitle 2a fixed static string? Or is it always coming after an empty line?