0

I need help in splitting two email address which are seperated by a Delimiter 'AND'. I have issue when splitting, when the email address has got the characters'AND' in the email id. For eg, if the email address that needs to be split is something like the below. There are no whitespaces between the two email address.

'[email protected]@yahoo.co.in', and the delimiter is'AND' 

In the above case, there seems to be three items extracted instead of two. Can someone please help me solve this. Thanks in Advance

5
  • 3
    Use " AND " as the separator (i.e. include the spaces) and you should be fine. Commented Nov 13, 2013 at 9:27
  • is the delimiter always AND? does it always have 1 white space surrounding the delimiter? Commented Nov 13, 2013 at 9:29
  • Thanks for the Instant response. Actually there are some emails, where there is no space in between two emailids. Only the 'AND' operator is present.So the issue. Commented Nov 13, 2013 at 9:31
  • Thanks for the response hovanessyan. There is no whitespace. Its always AND Commented Nov 13, 2013 at 9:31
  • if your email addresses are always consists AND with surrounded white spaces so it is very better to use " AND " including both sides white spaces Commented Nov 13, 2013 at 9:32

4 Answers 4

2

You can use " AND " as delimiter.

 String str="[email protected] AND [email protected]";
 String[] emailArr=str.split(" AND "); 

Or you can use following regex

  String str = "[email protected] AND [email protected]";
  Pattern p = Pattern.compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+
                                   (\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})");
  Matcher matcher = p.matcher(str);
  while (matcher.find()) {
     System.out.println(matcher.group(0));
  }

Out put

 [email protected]
 [email protected]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. But as specified earlier, there are no whitespaces in the delimiter.
1

Giving correct output

public class Test {
        public static void main(String args[]) {
            String text = "[email protected] AND [email protected] ";
            String[] splits = text.split(" AND ");
            for (int i = 0; i < splits.length; i++) {
                System.out.println("data :" + splits[i]);
            }
        }

    }

Output is

data :[email protected]    
data :[email protected] 

Comments

1

Use this :

String[] splits = text.split("\\s+AND\\s+");

3 Comments

Thanks for the response. I tried this, but it doesnt seem to work
I have written code assuming your string is enclosed in single quotes. If not, remove the substring part and try it out. I have edited the post. Check it out.
As I specified there is no whitespaces between the delimiter and the email address.I tried without the substring as well. But doesnt work.
0

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) the regular expression will be case sensitive

actually, the best is to use delimiters exression that you are sure will not be in the adress

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.