将段落拆分成句子,保留标点符号——不是重复的

Splitting paragraph into sentences keeping the punctuations - not a dup

这里有一点我再次使用带有 PHP preg_split() 函数的正则表达式卡住了。

代码如下:

preg_split('~("[^"]*")|[!?.।]+\s*|\R+~u', $paragraph, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

我正在尝试将段落拆分成句子。这段代码为我完成了工作。
这是我的

link

但是,现在我需要保持标点符号完整(问号、句号等)。

使用 PREG_SPLIT_DELIM_CAPTURE 应该已经完成​​了这项工作,但不知何故它不是那样工作的。我只得到句子,没有句号或问号。

您的要求不需要PREG_SPLIT_DELIM_CAPTURE。当您需要将它们作为单独的匹配项返回时,这会很有帮助。在这种情况下,您需要 \K:

<?php

var_dump(preg_split('~("[^"]*")|[!?.।]+\K\s*|\R+~u', <<<STR
hello! how are you? how is life
live life, live free. "isnt it?"
STR
, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));

输出:

array(5) {
  [0]=>
  string(6) "hello!"
  [1]=>
  string(12) "how are you?"
  [2]=>
  string(11) "how is life"
  [3]=>
  string(21) "live life, live free."
  [4]=>
  string(10) ""isnt it?""
}