I need to parse this document with email ids and convert them to a csv file...

source.

xxxx@xxx.com; hello@gmail.com; John (john@hotmail.com);

and so on.....lots of emails are there :D

output:

NickName, Email
,xxxx@xxx.com
,hello@gmail.com
John,john@hotmail.com

Please help me. This aint a homework. Im trying to recover my emails... hehe. I could write a regex to get the emails... but I need the nicknames too. but some emails doing have em.

please help :D

Edited 4 Years Ago by stevanity: n/a

You could split the string by the ';' caracter and if you found the '(' caracter it means that the name before it is a nickname and the name until the ')' caracter is a email.

heres a little idea of what it could look like, i didnt take the time to make it fetch the string from a file but it would be rather easy to implement as well!

good luck!

import java.util.regex.Pattern;


public class EmailParser {
	private static String s = "xxxx@xxx.com; hello@gmail.com; John (john@hotmail.com);";
	public static void main(String[] args) {
		System.out.println(parse(s));
	}
	
	public static String parse(String list){
		String[] sArray = list.split(";");
		String[] sArrayTwo;
		String result ="";
		for (int i = 0; i < sArray.length; i++) {
			if(sArray[i].contains("(")){
				sArrayTwo = sArray[i].split(Pattern.quote("("));
				result += sArrayTwo[0].trim() + ", " + sArrayTwo[1].substring(0,sArrayTwo[1].indexOf(")")).trim();
			}
			else{
				result += sArray[i].trim();
			}
			result += "\n";
		}
		return result;
	}

}

Outputs :

xxxx@xxx.com
hello@gmail.com
John, john@hotmail.com

Thanks... I coded the solution myself :D

Here's the code..

import java.util.*;
import java.io.*;

class EmailParser
{

  static class EmailID
  {
    String Nick;
    String id;
    
    public EmailID(String Nick, String id)
    {
      this.Nick=Nick;
      this.id=id;
    }
    
    public String toString()
    {
      return "Nick Name: "+Nick+"\nID: "+id;
    }
  }
  
  static EmailID parseEmail(String email)
  {
    String nick="";
    String id="";
    
    if(email.charAt(email.length()-1)==')')
    {
      String[] parts=email.split(" ");
      for(int i=0; i<=parts.length-2; i++)
      {
        nick=nick+parts[i]+" ";
      }
      nick=nick.trim();
      id=parts[parts.length-1].substring(1,parts[parts.length-1].length()-1);
    }
    else
    {
      id=email.trim();
      nick="";
    }
    return new EmailID(nick,id);
  }
  
  static void initializeCSV()
  {
    output.println("Nickname,E-mail Address");
  }
    
  
  static void writeToCsv(EmailID id)
  {
    System.out.println(id.Nick+","+id.id);
    output.println(id.Nick+","+id.id);
    count++;
  }
  
  public static void main(String arg[])
  {
    FileInputStream file = null;
    Scanner input=null;
    try
    {
      try
      {
        file = new FileInputStream(new File("emails"));
        input=new Scanner(file).useDelimiter(";");     
        csv=new FileOutputStream("mailids.csv");
        output=new PrintWriter(csv,true);
        while(input.hasNext()) 
        {   
        String email=input.next().trim();
        EmailID id=parseEmail(email);
        writeToCsv(id);
        //count++;
        }
      }
      finally
      {
        file.close();
      }
    }
    catch (Exception e)
    { 
    //e.printStackTrace();
    }
 
    System.out.println("Total Ids processed: "+count);
    
  }
  
  static FileOutputStream csv;
  static int count=0;
  static PrintWriter output;
}
This article has been dead for over six months. Start a new discussion instead.