0

How can I split a text into an array of sentences using regex?

Example Text:

This is test... Sentense number 2. Sentence number 3?

Expected output:

This is test... 

Sentense number 2.  

Sentence number 3?

1 Answers1

3

You could also use a BreakIterator:

  String s = "This is test... Sentense number 2. Sentence number 3?";
  BreakIterator bi = BreakIterator.getSentenceInstance(Locale.ENGLISH);
  bi.setText(s);
  int start = 0;
  int end = 0;
  while ((end = bi.next()) != BreakIterator.DONE) {
    System.out.println(s.substring(start, end));
    start = end;
  }
assylias
  • 321,522
  • 82
  • 660
  • 783