-1

first of all, I've been revieweing previous questions about splitting in java, I 've seen a lot of answers but none of what I've read solve my problem.

I'm trying to split the following string:

String toSplit = "hello@mail.com, \"hel,o\"@mail.info , not,hello@mail.com";

I would like to have , using the split function the following result:

hello@mail.com

"hel,o"@mail.info

not,hello@mail.com

as you can see, the , is what makes the differents string, but is also allowed to be included in the string, I've not ben able to find anything to put in the string.split function that works for me... any other solution not using the split function will also be valid

thanks in advance

talex
  • 17,973
  • 3
  • 29
  • 66
ICM
  • 1
  • 2

3 Answers3

0

If it is possible to change your split character, you could use a separator that is not a valid character for email such as ;

Your string would look like this: "hello@mail.com;"hel,o"@mail.info;not,hello@mail.com"

Fleezey
  • 156
  • 7
0

For your given String toSplit, I assumed that comma should be followed by space(s). Using that logic,

// String[] ans = toSplit.split("(,\\s+)");

String toSplit = "hello@mail.com, \"hel,o\"@mail.info , not,hello@mail.com";
        String[] ans = toSplit.split("(,\\s+)");
        for(String s : ans){
            System.out.println(s.trim());  //for trailing whitespaces
        }

Here's the output :

hello@mail.com
"hel,o"@mail.info
not,hello@mail.com
Uddipta_Deuri
  • 63
  • 1
  • 1
  • 7
0

I think Murat K gives you the answer.

You can also see this post and add space after coma in the regex

Java: splitting a comma-separated string but ignoring commas in quotes

Community
  • 1
  • 1
Pedroo
  • 11
  • 3