繁体   English   中英

PHP Regex 在一定数量的字符后匹配句号

[英]PHP Regex match full stop after certain number of characters

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

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 个除换行符以外的任何字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM