kivul
kivul

Reputation: 1158

Splitting string with similar starting pattern

So, I've been trying to split something I'm reading from a file. But everything that I've tried does not give me only the part that I want. What I have as string is this:

Scenario:
Bunch of stuf here
Just typing stuff for the example...

Scenario:
More stuff here
A lot more stuff here
XX123

I want to get everything from 'Scenario:' to 'XX123' Like this:

Scenario:
More stuff here
A lot more stuff here
XX123

The file that I'm reading from have a lot of those 'Scenarios:' and using Pattern from java doesn't give me only the part that I want. Instead it gives from the first 'Scenario:' it finds until 'XX123'

I also tried to use StringUtils.substringBetween, same result.

Thanks in advance

Upvotes: 1

Views: 47

Answers (1)

iluxa
iluxa

Reputation: 6969

The old-fashioned way to do it would look something like this:

String inputText;

String END_MARKER = "XXX123";

int indexOfEnd = inputText.indexOf(END_MARKER);
// search in reverse
int indexOfScenario = inputText.lastIndexOf("Scenario", indexOfEnd);

String result = inputText.substring(indexOfScenario, 
    indexOfEnd + END_MARKER.length());

Upvotes: 2

Related Questions