Click to See Complete Forum and Search --> : Read email address from String


idn
02-10-2006, 01:48 PM
Hi,

I have a log file and I need to extract an email address from the log file. So far I have the code:


int atPos = logLine.indexOf("@");

if (atPos > -1) {
endPos = logLine.indexOf(" ", atPos);

System.out.println(logLine.substring(atPos, endPos));
}


logLine is a string which is a line of the log file.

This code ouputs @foo.com, put I cant get the username part. The email address is in the middle of the string, and the log file changes format row to row (its more or a debig file, so theres no consistant formatting on where the email address will appear if there is one.

I really dont know how to look before the @ to get the username, so any help would be very welcome :)

BigDog
02-10-2006, 02:37 PM
You are better off using a regular expression here. If you are not familiar with regular expressions then you may have a bit of a learning curve. basically you can set up code that would look for some pattern that would match what an email address looks like. Just google "regular expressions" for info. String has a "matches" method that will take a regular expression, you can use this to parse the lines.