3 回答
TA贡献1864条经验 获得超2个赞
另一种选择是利用\G
迭代匹配来断言前一个匹配结束时的位置,并捕获捕获组中匹配点和 0+ 水平空白字符之后的值。
(?:\|\h*|\G(?!^))([^.\r\n]+)\.\h*
在零件
(?:
非捕获组\|\h*
匹配|
和 0+ 个水平空白字符|
或者\G(?!^)
在上一场比赛结束时断言位置)
关闭组(
捕获组 1 匹配除换行符- [^.\r\n]+
以外的任何字符 1 次以上.
)
关闭组\.\h*
匹配 1 个.
和 0+ 个水平空白字符
例如
$re = '/(?:\|\h*|\G(?!^))([^.\r\n]+)\.\h*/';
$str = '| This is one. This is two.
John loves Mary.| This is one. This is two.';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
print_r($matches);
输出
Array
(
[0] => Array
(
[0] => | This is one.
[1] => This is one
)
[1] => Array
(
[0] => This is two
[1] => This is tw
)
)
TA贡献2051条经验 获得超10个赞
这可以完成工作:
$str = '| This is one. This is two.';
preg_match_all('/(?:\s|\|)+(.*?)(?=[.!?])/', $str, $m);
print_r($m)
输出:
Array
(
[0] => Array
(
[0] => | This is one
[1] => This is two
)
[1] => Array
(
[0] => This is one
[1] => This is two
)
)
TA贡献1812条经验 获得超5个赞
我会保持简单,只需匹配:
\s*[^.|]+\s*
这表示匹配任何不包含管道或句号的内容,并且它还会在每个句子之前/之后修剪可选的空格。
$input = "| This is one. This is two.";
preg_match_all('/\s*[^.|]+\s*/s', $input, $matches);
print_r($matches[0]);
这打印:
Array
(
[0] => This is one
[1] => This is two
)
- 3 回答
- 0 关注
- 115 浏览
添加回答
举报