regex - How to read a paragraph from a file in java -


i have been given file has many paragraphs in it. output expecting read 1 paragraph @ time , 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);             }              (final string s : paragraph) {                  system.out.println(s);             } 

file (every paragraph has space of 2 characters before it, , there no blank line between paragraphs):

                      story

  her companions instrument set estimating sex remarkably solicitude motionless. property men why smallest graceful day insisted required. inquiry justice country old placing sitting ten age. looking venture justice in evident in totally ability. lose girl long of give.
  "trifling wondered unpacked ye @ he. in household certainty on tolerably smallness difficult. many no each next neat. put not enjoyment behaviour supposing. @ pulled object others."
  passage ten led hearted removal cordial. preference astonished unreserved mrs. prosperous understood middletons in conviction uncommonly do. supposing resolving breakfast or perfectly. drew hill mr. valley oh twenty direct me so.
  departure defective arranging rapturous did believing him had supported. family months lasted simple set nature vulgar him.   "picture attempt joy excited ten carried manners talking how. suspicion neglected resolving agreement perceived @ an."

however, not achieving desired output. paragraph variable contains 2 values

  1. the title of file
  2. the rest of contents of file.

i guess, regex trying use here not working. regex gathered here. splitting text paragraphs regex java

i using java8.

you can used scanner delimiter, iterating on text. example:

scanner scanner = new scanner(text).usedelimiter("\n  "); while (scanner.hasnext()) {     string paragraph = scanner.next();     system.out.println("# " + paragraph); } 

the output is:

#                       story  # companions instrument set estimating sex remarkably solicitude motionless. property men why smallest graceful day insisted required. inquiry justice country old placing sitting ten age. looking venture justice in evident in totally ability. lose girl long of give. # "trifling wondered unpacked ye @ he. in household certainty on tolerably smallness difficult. many no each next neat. put not enjoyment behaviour supposing. @ pulled object others." # passage ten led hearted removal cordial. preference astonished unreserved mrs. prosperous understood middletons in conviction uncommonly do. supposing resolving breakfast or perfectly. drew hill mr. valley oh twenty direct me so. # departure defective arranging rapturous did believing him had supported. family months lasted simple set nature vulgar him.   "picture attempt joy excited ten carried manners talking how. suspicion neglected resolving agreement perceived @ an." 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -