Gilles
Gilles

Reputation: 369

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 ~~.

Upvotes: 1

Views: 47

Answers (2)

Mena
Mena

Reputation: 48404

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".

Upvotes: 0

vks
vks

Reputation: 67968

You can split by

\b~\b

See demo.

https://regex101.com/r/t3D2Jp/1

You can use

(?:^|\b)~(?:$|\b)

if you want to remove trailing ones too

Upvotes: 2

Related Questions