PHP 正则表达式匹配一定数量的字符后的句号
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,})
模式适合你。见 regex demo.
思路是求行长,除以2得到中间的char,得到的值减去1或2,代替中限制量词中的30
上面的图案。
图案详情
.{30,}?
- 除换行符以外的任何 30 个字符或更多,但尽可能少,
\K
- 匹配重置运算符,省略目前匹配的文本
\.
- 一个点
(?=.{30,})
- 一个积极的前瞻,要求在当前位置的右侧存在至少 30 个除换行符之外的任何字符。
我不知道如何使用正则表达式来做到这一点。我想匹配句子中一定数量的字符后的句号。
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,})
模式适合你。见 regex demo.
思路是求行长,除以2得到中间的char,得到的值减去1或2,代替中限制量词中的30
上面的图案。
图案详情
.{30,}?
- 除换行符以外的任何 30 个字符或更多,但尽可能少,\K
- 匹配重置运算符,省略目前匹配的文本\.
- 一个点(?=.{30,})
- 一个积极的前瞻,要求在当前位置的右侧存在至少 30 个除换行符之外的任何字符。