Q:

Java split regexp when delimiter is part of the data

Sorry if this question has already been solved, or closed but I have been searching for long without an answer.
I have to split lines I am receiving from an external systems, using the ~ delimiter.
I have an issue because some data contain ~~ (~ repeated twice) and in this case the data must not be split.
So if I receive A~B~C~~C~D I want this split back:
A, B, C~~C, D
I cannot figure out what regular expression I have to used not to split ~~.

A:

You can use (?<!~)~(?!~) with a negative look-ahead and look-behind for ~. 
Example
String test = "A~B~C~~D~E";
System.out.println(
    Arrays.toString(
        test.split("(?<!~)~(?!~)")
    )
);

Output
[A, B, C~~D, E]

This should also work with more than two consecutive ~s, e.g. with "A~B~C~~~D~E".

