0

I'm trying to create a parser in Java that would help me to get some details from a text file.

The data in the file looks like this, but with more entries:

. 
http://www.someurl1.com/
PERSONAL ADDRESS: Mozart, W.A.; Some address 1, Austria; email: [email protected]

. 
http://www.someurl2.com/
PERSONAL ADDRESS: Beethoven, L.V.; Some address 2, Germany; email: [email protected]

As you can see, the data always respects a pattern, and what I would like to get is just the name and the e-mail for every entry. A possible good output would be this:

Mozart, W.A. ; [email protected]
Beethoven, L.V. ; [email protected]

Every entry starts with a . followed by a space in the first line. Then in the next line above the dot, there's the URL. In the following line, there's more data: name, address and e-mail, all separated by a ;.

This isn't hard but I'm having some troubles starting. I've created a Main class in which I read the text file to a String. But then I really don't know what's the best way to parse something like this in Java, if I should try to use regular expressions or just get looking for the ;.

3
  • I've started by creating a Main class in which I read the file to a String. But then I'm not sure how to parse it, if I should use regular expressions or just simply doing it the "hard" way. Commented Sep 8, 2014 at 18:19
  • @gd.silva Please update your question with what you have already tried. Commented Sep 8, 2014 at 18:19
  • If you have tried something and facing issues, you can post it here. Should be quite straight forward unless you have some complex requirement. Use a file reader to read each line and split based on the separator to a list and then use whatever data is needed from the list. Commented Sep 8, 2014 at 18:20

2 Answers 2

5

Read in the text file line by line and then do an action based on that line.

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
   // process the line.
   if (line.equals(". "))
   {
       // Do something with first line
       line = br.readLine()
       // Do something with second line
       line = br.readLine()
       // Split up the third line by space 
       String split[]= StringUtils.split(line); // split[1] = "Mozart," so you may need to do a little more work there
   }
}
br.close();
Sign up to request clarification or add additional context in comments.

Comments

1

Use split strings for name is easy, then use regular expression to catch the email part! There are alot of examples, here is one of them

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.