Results 1 to 6 of 6

Thread: Regex to find email address

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Regex to find email address

    Hello,
    I use a regular expression to find all email addresses that are found in a text file. Here is my source code
    Code:
    String txt;
    ArrayList ls = <String> new ArrayList <String>(0);
    FileReader in;
    try
    	{
    	in = new FileReader("/ locations / file.txt");
    	}
    catch (FileNotFoundException e)
    	{
    	throw new Exception("Unable to open file"E);
    	}
    / / Iterate over the file
    int c;
    try
    	{
    	while ((c = in.read()) != -1)
    		{
    		txt = txt + (char) c;
    		}
    	}
    catch (IOException e)
    	{
    	throw new Exception("Unable to read file"E);
    	}
     
    / / Definition of regexp
    AChercher Pattern = Pattern.compiles("[_A-Za-z0-9-] + (\\. [_A-Za-z0-9-]+)*@" +
    		"[A-Za-z0-9-] + (\\. [A-Za-z0-9-]+)*(\\. [_A-Za-z0-9-] +) ");
    mth = aChercher researcher.matcher(txt);
    		
    / / Search the address
    while (researcher.find())
    	{
    	String email = researcher.group();
    ls.add(email);
    	}
    But this is not working for me, I request that you just go through the code and see if you have missed something. And please guide me if I have mistaken somewhere.
    Last edited by TechGate; 05-02-2010 at 01:57 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Regex to find email addresses

    Hello,
    Can you show the code to display your list?
    - It is better to define your list as a List:
    Code:
          List list = <String> new ArrayList <String>();
    - Why put all your file in a buffer character by character? You could read line by line and do your search loop line.
    Display the code and then we can help you.

  3. #3
    Join Date
    Dec 2009
    Posts
    263

    Re: Regex to find email addresses

    Hello,
    To test, I write System.out.println facility directly via:
    Code:
    for (<String> Iterator it = list.ietrator(); It.hasNext();)
    	{
    	System.out.System.out.println(it.next());			
    	}
    But if I display in a JTable and the result is the same. Character by character or line back to the same right? I suppose the method that returns line by line reads the character 1 by 1 to find a line so it should not be any difference in speed and resource use.

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Regex to find email addresses

    Hello,
    Instead of declaring the string like this:
    Code:
    String text;
    / / I:
    String text = "";
    I think it is better to declare the string in this way
    Code:
    text = text + (char) c;
    / / Has returned to
    text = null + (char) c;
    / / 1st character
    I hope this is going to help you. If you have any more queries then post with them.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Regex to find email addresses

    Hello,
    If you want to fill a string that contains the entire contents of the file
    Code:
    text = text + (char) c;
    This creates a new string to each character! Use a StringBuilder and append ()!
    Why not read character by character:
    - If you want to read one character at a time, why not! But you want to read lines.
    - Then you go to store all your files when you have no need, even if you have lots of memory.
    - With a good old BufferedReader, a loop readline () and search the current line is cleaner.
    - Your file is small, your code works, but it is always better to be ready for the large files.

  6. #6
    Join Date
    Dec 2009
    Posts
    263

    Re: Regex to find email addresses

    Hello,
    Thanks for the replies but actually I'll use StringBuilder. This is not a bad idea to research the addresses in the line and then move to the next line. But I will have a design issue, reading this in a class and research in another. I use the pattern 'template method'. But I can look to change the design to allow it. If you have any other alternative solution for doing the same then please do post back.

Similar Threads

  1. IP address regex problem with preg_match_all
    By Leilaa in forum Software Development
    Replies: 2
    Last Post: 12-04-2012, 06:54 PM
  2. How to find actual owner of email by IP Address
    By Bacchus in forum Technology & Internet
    Replies: 4
    Last Post: 03-11-2010, 05:44 PM
  3. Regex to verify email address
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 03:50 AM
  4. How to find someones myspace email address
    By redcross in forum Technology & Internet
    Replies: 4
    Last Post: 20-11-2009, 09:34 AM
  5. How to find IP address in email
    By Deepesh in forum Technology & Internet
    Replies: 5
    Last Post: 28-04-2009, 09:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,870,328.56578 seconds with 17 queries