You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP正则如何用正向预查避免将数字后的点判定为句子结束符

正则句子拆分优化方案

最终可用正则

(.*?(?:(?<!\d)\.|\?|!))(?: |$)

规则说明

  • 新增(?<!\d)负向零宽回顾断言,限制只有非数字开头的英文句号才会被判定为句子结束标识,完美跳过1.、2.这类序号后的句号
  • 保留原有问号、感叹号的匹配逻辑,不影响非句号结尾的句子拆分

测试效果

针对示例文本1. Today is the greatest. You are the greatest.,匹配后的捕获结果如下:

Match {
  1. Today is the greatest.
  2. You are the greatest.
}

扩展适配(可选)

如果你的文本中存在3.14这类带小数点的数字,可以使用进一步优化的正则,同时排除小数点位的误识别:
(.*?(?:(?<!\d\.\d)(?<!\d)\.|\?|!))(?: |$)

内容的提问来源于stack exchange,提问作者Natdrip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 22:06:07