3

I have some string like this:

Title
First
Second
Third

Title 2
First 
Second
Third

I want to be able to jump to Title 2 directly and then grab First, Second, Third. I know how to grab the things (using Pattern/Matcher) but not sure how to jump to Title 2.

1
  • Clarify jump to Title 2? Is Title 2 a fixed static string? Or is it always coming after an empty line? Commented Nov 28, 2015 at 5:21

2 Answers 2

1

This regular expression will grab 'Title 2' as well as the 3 lines below it (as you want), and put them in match groups. You can read the match groups using Pattern/Matcher.

Title 2\n(.*)\n(.*)\n(.*)

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

@Mariano yes, but what about the case, then there is not a single \n, but \r\n or the amty line contains whitespaces? Sure, it's possible to combine all this cases into the sigle pattern, but for that we use to know the format of the given string
@Mariano thx, nice solution, but anyway, the problem with \\R{2,} is the fact, that you will get only 2 strings, each with all the values of the group, but it could be not the expected result and you'll need to sptit 'em once more. It depends on the way, how are this values processed after splitting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.