"split string into sentences using regex" Answer’s


As it should be expected, any sort of natural language processing is not a trivial task. The reason for it is that they are evolutionary systems. There is no single person who sat down and thought about which are good ideas and which - not. Every rule has 20-40% exceptions. With that said the complexity of a single regex that can do your bidding would be off the charts. Still, the following solution relies mainly on regexes.

  • The idea is to gradually go over the text.
  • At any given time, the current chunk of the text will be contained in two different parts. One, which is the candidate for a substring before a sentence boundary and another - after.
  • The first 10 regex pairs detect positions which look like sentence boundaries, but actually aren't. In that case, before and after are advanced without registering a new sentence.
  • If none of these pairs matches, matching will be attempted with the last 3 pairs, possibly detecting a boundary.

As for where did these regexes come from? - I translated this Ruby library, which is generated based on this paper. If you truly want to understand them, there is no alternative but to read the paper.

As far as accuracy goes - I encourage you to test it with different texts. After some experimentation, I was very pleasantly surprised.

In terms of performance - the regexes should be highly performant as all of them have either a A or Z anchor, there are almost no repetition quantifiers, and in the places there are - there can't be any backtracking. Still, regexes are regexes. You will have to do some benchmarking if you plan to use this is tight loops on huge chunks of text.

Mandatory disclaimer: excuse my rusty php skills. The following code might not be the most idiomatic php ever, it should still be clear enough to get the point across.

function sentence_split($text) {
    $before_regexes = array('/(?:(?:['"„][.!?…]['"”]s)|(?:[^.]s[A-Z].s)|(?:b(?:St|Gen|Hon|Prof|Dr|Mr|Ms|Mrs|[JS]r|Col|Maj|Brig|Sgt|Capt|Cmnd|Sen|Rev|Rep|Revd).s)|(?:b(?:St|Gen|Hon|Prof|Dr|Mr|Ms|Mrs|[JS]r|Col|Maj|Brig|Sgt|Capt|Cmnd|Sen|Rev|Rep|Revd).s[A-Z].s)|(?:bApr.s)|(?:bAug.s)|(?:bBros.s)|(?:bCo.s)|(?:bCorp.s)|(?:bDec.s)|(?:bDist.s)|(?:bFeb.s)|(?:bInc.s)|(?:bJan.s)|(?:bJul.s)|(?:bJun.s)|(?:bMar.s)|(?:bNov.s)|(?:bOct.s)|(?:bPh.?D.s)|(?:bSept?.s)|(?:bp{Lu}.p{Lu}.s)|(?:bp{Lu}.sp{Lu}.s)|(?:bcf.s)|(?:be.g.s)|(?:besp.s)|(?:betbsbal.s)|(?:bvs.s)|(?:p{Ps}[!?]+p{Pe} ))Z/su',
        '/(?:(?:[[(]*...[])]* ))Z/su',
        '/(?:(?:[.!?…]+p{Pe} )|(?:[[(]*…[])]* ))Z/su',
    $after_regexes = array('/A(?:)/su',
    $is_sentence_boundary = array(false, false, false, false, false, false, false, false, false, false, true, true, true);
    $count = 13;

    $sentences = array();
    $sentence = '';
    $before = '';
    $after = substr($text, 0, 10);
    $text = substr($text, 10);

    while($text != '') {
        for($i = 0; $i < $count; $i++) {
            if(preg_match($before_regexes[$i], $before) && preg_match($after_regexes[$i], $after)) {
                if($is_sentence_boundary[$i]) {
                    array_push($sentences, $sentence);
                    $sentence = '';

        $first_from_text = $text[0];
        $text = substr($text, 1);
        $first_from_after = $after[0];
        $after = substr($after, 1);
        $before .= $first_from_after;
        $sentence .= $first_from_after;
        $after .= $first_from_text;

    if($sentence != '' && $after != '') {
        array_push($sentences, $sentence.$after);

    return $sentences;

$text = "Mr. Entertainment media properties. Fairy Tail 3.5 and Tokyo Ghoul.";
Wednesday, March 31, 2021
answered 11 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :