PHP 正则表达式在一定数量的字符后匹配句号



我不知道如何使用正则表达式做到这一点。我想在句子中一定数量的字符之后匹配句号。

this is a long sentence. it contains a few full stops in it. I want to match the full stop after the halfway point.
this sentence is shorter. it also contains full stops but not many.

它也不应该与最后一个句号匹配。它应该与第一句中的第二个句号匹配,而在第二句中不匹配。所以匹配应该看起来像这样:

this is a long sentence. it contains a few full stops in it[.] I want to match the full stop after the halfway point.
this sentence is shorter. it also contains full stops but not many.   [no match]

有没有办法做到这一点?我有一些与此类似的内容根本不起作用:

/[.]{20,}/

根据您的反馈,

.{30,}?K.(?=.{30,})

模式适合您。请参阅正则表达式演示。

这个想法是找到线长,除以 2 得到中间的字符,然后从获得的值中减去 1 或 2,并使用它代替上面模式中限制量词中的30

图案详细信息

  • .{30,}?- 除换行符字符以外的任何 30 个字符或更多,但尽可能少,
  • K- 匹配重置运算符,省略到目前为止匹配的文本
  • .- 一个点
  • (?=.{30,})- 一个积极的展望,要求至少存在 30 个除换行符以外的任何字符紧邻当前位置右侧。

最新更新