0

How to extract email address from String like below with delimiter as "AND" ?

String str="abb@AND.comANDbbb.comANDccc@xxd.AND";
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
  • 2
    Only 4 programming languages tagged with this? You don't want more? – devnull Nov 13 '13 at 10:47
  • 1
    There is a solution for C++: http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c, But do not store the data so - get familiar with JSON! –  Nov 13 '13 at 10:48
  • `String` + C tag? come on... you might aswell add a Lisp tag – Elias Van Ootegem Nov 13 '13 at 10:59
  • 1
    Splitting the string is easy if the delimiter cannot be contained in any of the addresses. If you need to support escape sequences etc. it gets slightly more complex. And if you want to formally validate that the resulting strings are actually email addresses, you'll get lost in a jungle of various RFC's (which are sometimes deliberately ignored by real-world providers) – Hulk Nov 13 '13 at 10:59

2 Answers2

3

Try with following code segment,

public class TokenizerTest
{
    private final String testStr = "abb@AND.comANDbbb.comANDccc@xxd.AND";

    public static void main(String[] args)
    {
        TokenizerTest tst = new TokenizerTest();
        tst.tokenize();
    }

    public void tokenize()
    {
        if(testStr != null)
        {
            Pattern p = Pattern.compile("[*]*AND*[^.AND]");
            String[] tokens = testStr.split(p.pattern());
            for(String token:tokens)
            {
                System.out.println(token);
            }
        }
    }
}

It results in

abb@AND.com
bb.com
cc@xxd.AND
  • if i try above code with String "anandc@AND.comANDxyz@yahoo.co.in"...its output is anandc@AND.com yz@yahoo.co.in // i.e.Missing starting "x" – Kamlesh Arya Nov 13 '13 at 11:39
  • You need to tweak that pattern to suite your input needs –  Nov 13 '13 at 13:43
1

Short answer: Computer says no.

Based on the string you provided, it can probably not be done in a reliable way, since it is not clear what is an email address, and what is a delimiter. In fact, it is difficult to extract more than one or two valid email-addresses at all from that line.

I recommend you first check your dataset to see if it is corrupted, then find a way to use a different delimiter.

Kjartan
  • 18,591
  • 15
  • 71
  • 96