Best way to extract specific paragraph from file data

后端 未结 2 563
予麋鹿
予麋鹿 2021-01-21 18:15

Hi i am looking to find out the best way to extract specific paragraph from file using java.

From the following data i need to extract data from \"D & A\"

相关标签:
2条回答
  • 2021-01-21 18:53

    I would read in the file line by line, something like this tutorial.

    You can then check if the line contains a certain string.

    boolean readFollowingLines = false;
    ArayList<String> paragraph=new ArayList<String>();
    if( string.indexOf("1- End") > 0 ) // maybe >= 0, not shure
        readFollowingLines = false;
    if (readFollowingLines)
       paragraph.add(string);
    if( string.indexOf("D and A") > 0 ) // maybe >= 0, not shure
       readFollowingLines = true;
    

    If you want more then one paragraph you need to extend this a little. Anyway, I'd probably do it something like this

    0 讨论(0)
  • 2021-01-21 19:12

    for an input like this

    Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata Testingdata

    D and A
    Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1- End

                                                  Date 11/30/11           Page    2
    

    D and A
    Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2

                                                  Date 11/30/11           Page    3
    

    D and A
    Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2- End

    The following Regex will help you out

        String input="";
        BufferedReader br = new BufferedReader(new FileReader("path/to/text/file")); //file path will be something like "D:/test1.txt" or "/home/naishe/test1.txt"
        String line;
        while((line = br.readLine()) != null) {
            input += line+"\n";
        }
    
        Pattern p = Pattern.compile("(D and A\\s).*?(Testing(1|2)\\- End)");
        Matcher m = p.matcher(input);
        while(m.find()){
            System.out.println("MATCHED:\n" + m.group());
        }
    

    gives

    MATCHED:
    D and A
    Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1Testing1 Testing1 Testing1 Testing1 Testing1 Testing1 Testing1- End

    MATCHED:
    D and A
    Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2 Testing2- End

    0 讨论(0)
提交回复
热议问题