Reputation: 1025
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 ;
.
Upvotes: 0
Views: 11056
Reputation: 74
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
Upvotes: 1
Reputation: 1786
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();
Upvotes: 5