/    Sign up×
Community /Pin to ProfileBookmark

Read email address from String

Hi,

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

[CODE]
int atPos = logLine.indexOf(“@”);

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

System.out.println(logLine.substring(atPos, endPos));
}
[/CODE]

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 ?

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@BigDogFeb 10.2006 — 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.
×

Success!

Help @idn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...