10

I want to divide a text into sentences. A sentence ends with (dot) or ? or ! followed by one or more whitespace characters followed and the next sentence starts with an uppercase letter.

For example:

First sentence. Second sentence!

How can I do that?

4
  • 20
    How would you handle this: "When he came to the house, he shouted "Hey! Anybody there?", and then opened the door and went in." Or you you have very strict control over the input? Commented Feb 10, 2011 at 12:46
  • I added a regular expression in my answear that considers this case. Ref regexlib.com/REDetails.aspx?regexp_id=2355
    – Thea
    Commented Feb 10, 2011 at 12:57
  • @Fredrik Mörk Your example is a particularly interesting case. I would handle as a single sentence
    – Lato
    Commented Feb 10, 2011 at 13:15
  • It gets even more complex: "They went down to Main St. and talked to Dr. Smith about the $127.99 bill. 101 Dalmatians. In that case... I just don't know." Commented Jul 7 at 8:12

4 Answers 4

35

You can split on a regular expression that matches white space, with a lookbehind that looks for the sentence terminators:

string[] sentences = Regex.Split(input, @"(?<=[\.!\?])\s+");

This will split on the white space characters and keep the terminators in the sentences.

Example:

string input = "First sentence. Second sentence! Third sentence? Yes.";
string[] sentences = Regex.Split(input, @"(?<=[\.!\?])\s+");

foreach (string sentence in sentences) {
  Console.WriteLine(sentence);
}

Output:

First sentence.
Second sentence!
Third sentence?
Yes.

Note:

This solution only addresses the simple case outlined in the question. To do this for any kind of sentence, or for other languages, the solution would be vastly more complicated. Most likely it's not possible to do something that can handle any sentence, even just in English, considering that the sentence might even be incorrectly written or have misplaced punctuation.

8
  • 1
    thank you thank you thank you !!!!!!!!!!!!! thank you thank you thank you ..thanks a ton...needed this badly..was struggling with string.split() function that aloows only single character. Commented Jan 10, 2013 at 9:07
  • 1
    Be careful because this wouldn't work for things like "Hello Mr. & Mrs. Smith".
    – Yodacheese
    Commented Nov 7, 2013 at 8:40
  • @Yodacheese: Yes, that is true. It would require a vastly more advanced analysis of the meaning of the sentence to catch when a period actually ends a sentence. Consider for example the sentences "The honorific is Mr. Smith is the surname.".
    – Guffa
    Commented Nov 7, 2013 at 14:17
  • you can add ellipsis: @"(?<=([\.!\?])|(\.{3}))\s+"
    – Horia Toma
    Commented Jun 2, 2016 at 21:42
  • This is a really bad solution
    – A X
    Commented Feb 17, 2021 at 0:23
6

What languages do you want to support? For example, in Thai there are no spaces between words and sentences are separated with space. So, in general, this task is very complex. Also consider the useful comment by Fredrik Mörk.

So, at first you need to define set of rules on what "sentence" is. Then you are welcome to use one of the suggested solutions.

2
  • 1
    I want to support mainly English. There are a lot of particular state. for example i have to consider Abbreviations (Dr. Jekyll)
    – Lato
    Commented Feb 10, 2011 at 13:22
  • This should be a comment since there is no answer here, only ask for an update in the question. Commented May 31, 2021 at 13:25
4

Try this (MSDN)

char[] separators = new char[] {'!', '.', '?'};
string[] sentences1 = "First sentence. Second sentence!".Split(separators);
//or...
string[] sentences2 = "First sentence. Second sentence!".Split('!', '.', '?');
3

Have you tried String.Split()? See the docs about it here

1
  • 4
    yes i did, but it is not sufficient. In text there are links. (Ex: www.mysite.it)
    – Lato
    Commented Feb 10, 2011 at 13:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.