-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 = "[email protected], \"hel,o\"@mail.info , not,[email protected]";

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

[email protected]

"hel,o"@mail.info

not,[email protected]

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: "[email protected];"hel,o"@mail.info;not,[email protected]"

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 = "[email protected], \"hel,o\"@mail.info , not,[email protected]";
        String[] ans = toSplit.split("(,\\s+)");
        for(String s : ans){
            System.out.println(s.trim());  //for trailing whitespaces
        }

Here's the output :

[email protected]
"hel,o"@mail.info
not,[email protected]
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