0

I need to parse string created from email content and extract data. Structure of a email looks like this:

Some email text..............
//these properties I have to extract
Name: SomeName
Surname: SomeSurname
Email: SomeEmail
Adress: some adress
etc...
Some email text................

Do you know some techniques, how to parse this string and extract data? I find some way but it is defective, becouse it not works when value has whitespace, just like Adress above.

    String searchText = "Name:";
    int lastIndexOf = content.lastIndexOf(searchText);
    String substring = content.substring( 1 + lastIndexOf + searchText.length());
    String substring1 = substring.substring(0, substring.indexOf(" "));
Damian U
  • 371
  • 2
  • 19

1 Answers1

0

Here's the code for your solution:

    FileReader file;
    String sCurrentLine;
    String searchString = "Name";
    try {
        file = new FileReader("D:\\junk\\Sample.txt");

        BufferedReader br = new BufferedReader(file);

        br = new BufferedReader(new FileReader("D:\\junk\\Sample.txt"));

        while ((sCurrentLine = br.readLine()) != null) {

            String splitString = sCurrentLine.substring(0, sCurrentLine.indexOf(':'));
            splitString = splitString.replace(" ", "");
            if (splitString.equalsIgnoreCase(searchString)) {
                System.out.println(sCurrentLine.substring(sCurrentLine.indexOf(':')).replace(":",""));
                break;
            }

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Let me explain the code: Here we are taking input from a text file. The text file contains the key-value mappings Once we read the file, we parse it into a String in this case sCurrentLine

Then, we first split the String from the ':' and then we remove any blank spaces and then store the split String in splitString This becomes out key without spaces.

Then we match the splitString with the searchString. If matched, then we print the part of the String which is after the ':'.

Hope it helps!

  • Yes, read line by line would be solution but in my case I can't read line by line becouse I get this String from javax.mail.Message (as content), so this String have only one line. – Damian U Jan 12 '17 at 15:00
  • In that case, you need to convert the javax.mail.Message to String and then follow the solution approach provided above. In that case the while -loop is not needed. Only the logic inside the while-loop can be used,where sCurrentLine will be the String you will get after convert the javax.mail.Message feed. Please refer to the following links for inspiration: http://stackoverflow.com/questions/11240368/how-to-read-text-inside-body-of-mail-using-javax-mail and http://stackoverflow.com/questions/13474705/reading-body-part-of-a-mime-multipart – Sourav Purakayastha Jan 12 '17 at 15:13
  • If the string really is only one line with no newlines, I don' t know how you tell where one field ends and the next begins. But if it's a single string that contains multiple lines, as displayed above, you can use a [StringReader](http://docs.oracle.com/javase/8/docs/api/java/io/StringReader.html) (instead of FileReader) to read the lines in the string. This won't be the most efficient approach, but it's easy. – Bill Shannon Jan 12 '17 at 23:31
  • And depending on the exact format of the data, you might be able to use the [InternetHeaders](https://javamail.java.net/nonav/docs/api/javax/mail/internet/InternetHeaders.html) class to read the data. – Bill Shannon Jan 12 '17 at 23:31