www.webdeveloper.com
Results 1 to 2 of 2

Thread: Read email address from String

  1. #1
    Join Date
    Dec 2005
    Posts
    108

    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));
                        }
    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

  2. #2
    Join Date
    Sep 2005
    Location
    Chicago, Il
    Posts
    144
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles