Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string by triple dots or horizontal ellipsis

I am facing problems when trying to split a String by "..."

    String text ="Here…It is safer.";

I tried:

String [] output = text.split("[\\...]");
String [] output = text.split("\\.");

and many others, but I haven't found the solution yet.

I know that the question is very simple, but I will be happy If somebody can explain how should I make it work.

like image 596
Pencho Vlaskovski Avatar asked Oct 21 '25 14:10

Pencho Vlaskovski


2 Answers

Regex for matching three dots is \\.{3} or \\.\\.\\. or [.][.][.] or \\Q...\\E.

Both [\\...] and \\. match a single dot, because repeated characters inside a character class are treated as a single character.

Horizontal ellipsis is a different character. It is not a metacharacter in regex language, so it can be matched directly with no escaping:

String [] output = text.split("…");
like image 164
Sergey Kalinichenko Avatar answered Oct 23 '25 04:10

Sergey Kalinichenko


In general, you can use

String[] chunks = text.split("…|\\.{3}");

To also remove the enclosing whitespace:

String[] chunks = text.split("\\s*(?:…|\\.{3})\\s*");

See this regex demo.

If you need to make sure the triple dots are NOT enclosed with other dot chars, you can add lookarounds:

String[] chunks = text.split("\\s*(?:…|(?<!\\.)\\.{3}(?!\\.))\\s*");

Details:

  • \s* - zero or more whitespaces
  • (?:...) - a non-capturing group
  • - an ellipsis
  • | - or
  • (?<!\.) - a negative lookbehind that fails the match if there is a dot char immediately to the left of the current location
  • \.{3} - triple dots
  • (?!\.) - a negative lookahead that fails the match if there is a dot char immediately to the right of the current location.

See a Java demo:

String text = "Here…It is safer... The end.";
String[] chunks = text.split("\\s*(?:…|\\.{3})\\s*");
System.out.println(Arrays.toString(chunks));
// => [Here, It is safer, The end.]
like image 24
Wiktor Stribiżew Avatar answered Oct 23 '25 03:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!