I'm trying to extract a sequence of unpredictable text from the middle of a formatted string. Here is a an example of what my string might look like:
THIS PART NEVER CHANGES
Payload
UppErAndLowerCaseLetters
andDigitsNotPredictable
ButDoesIncludeLineBreaks
OtherStuffThatIDon'tWant
Note that there are line breaks here that must be preserved. In this example, I want to capture in a String variable this text:
Payload
UppErAndLowerCaseLetters
andDigitsNotPredictable
ButDoesIncludeLineBreaks
So, my "delimiters" are the header part THIS PART NEVER CHANGES
at the beginning and the double line break at the end. That's the tricky part. How do I write my regular expression to identify a double line break, but exclude a single line break? Here is what I have:
String payload = "THIS PART NEVER CHANGES" +
System.getProperty("line.separator") +
"(.+?)" +
System.getProperty("line.separator") +
System.getProperty("line.separator");
BufferedFileReader bfr = new BufferedFileReader();
String file_contents = bfr.readFileToString(myFile);
Pattern pattern = Pattern.compile(payload);
Matcher matcher = pattern.matcher(file_contents);
while (matcher.find())
System.out.println(matcher.group());
This almost works. If I take out the last System.getProperty("line.separator")
from the payload string, I get the first line from the payload. When I leave it in, I get nothing.
Can anyone tell me what I am doing wrong? Thanks!