I have been given a file which has many paragraphs in it. The output I am expecting is that I read one paragraph at a time and perform operations on it.
final String PARAGRAPH_SPLIT_REGEX = "(?m)(?=^\\s{4})";
String currentLine;
final BufferedReader bf = new BufferedReader(new FileReader("filename"));
currentLine = bf.readLine();
final StringBuilder stringBuilder = new StringBuilder();
while(currentLine !=null) {
stringBuilder.append(currentLine);
stringBuilder.append(System.lineSeparator());
currentLine = bf.readLine();
}
String[] paragraph= new String[stringBuilder.length()];
if(stringBuilder!=null) {
final String value = stringBuilder.toString();
paragraph = value.split(PARAGRAPH_SPLIT_REGEX);
}
for (final String s : paragraph) {
System.out.println(s);
}
File (Every paragraph has a space of 2 characters before it, and there is no blank line between paragraphs):
Story
Her companions instrument set estimating sex remarkably solicitude motionless. Property men the why smallest graceful day insisted required. Inquiry justice country old placing sitting any ten age. Looking venture justice in evident in totally he do ability. Be is lose girl long of up give.
"Trifling wondered unpacked ye at he. In household certainty an on tolerably smallness difficult. Many no each like up be is next neat. Put not enjoyment behaviour her supposing. At he pulled object others."
Passage its ten led hearted removal cordial. Preference any astonished unreserved mrs. Prosperous understood middletons in conviction an uncommonly do. Supposing so be resolving breakfast am or perfectly. Is drew am hill from mr. Valley by oh twenty direct me so.
Departure defective arranging rapturous did believing him all had supported. Family months lasted simple set nature vulgar him. "Picture for attempt joy excited ten carried manners talking how. Suspicion neglected he resolving agreement perceived at an."
However, I am not achieving the desired output. The paragraph variable contains only two values
- The title of the file
- The rest of the contents of the file.
I guess, the regex I am trying to use here is not working. The regex I gathered from here. Splitting text into paragraphs with regex JAVA
I am using java8.