簡體   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