-1

I extracted some text from a text file but now I want only some specific words from that text.

What I have tried is read from that text file and I have searched by using keyword:

    FileReader fr = new 
    FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;

    String keyword = "dba COPIEFacture ";

    while ((s = br.readLine()) != null) {
        if (s.contains(keyword)) {
            System.out.println(s);

I got Output like this: dba COPIEFacture du 28/05/2018 n° 10077586115Récapitulatif de vote facture

But I want only 28/05/2018 This so please help me

Guillermo Cacheda
  • 2,162
  • 14
  • 23
Deo
  • 45
  • 8

2 Answers2

0

You'll need to use String manipulation methods.

It's difficult to know the best way to do it without seeing other outputs, but you could probably use split() and indexOf() to retrieve the date.

There are other, probably more complex, methods. For example, here's a StackOverflow answer about retrieving dates from strings using a regex pattern.

Guillermo Cacheda
  • 2,162
  • 14
  • 23
0

This will do the trick.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {

   public static void main(String[] args) {

    FileReader fr;
    String keyword = "dba COPIEFacture du ";
     String textToFind = "28/05/2018"; // The length usually will not 
                                       // change.You can use value 
                                       // 10(length) instead
    StringBuilder sb = new StringBuilder();
    try {
        fr = new FileReader("D:\\PDFTOEXCEL\\Extractionfrompdf.txt");

        int i;
        while ((i = fr.read()) != -1) {
            sb.append((char) i);
        }

        int start = sb.indexOf(keyword) + keyword.length();
        int end = start + textToFind.length();

        System.out.print(sb.substring(start, end));   //output: 28/05/2018

        fr.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }
 }
DASH
  • 124
  • 5