我无法弄清楚如何使用正则表达式执行此操作。我希望在句子中的一定数量的字符后匹配句号。
\d
它也不应该匹配上一个句号。它应与第一句中的第二个句号匹配,并且在第二个句子中不匹配。所以比赛应该是这样的:
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]
答案 0 :(得分:1)
.{30,}?\K\.(?=.{30,})
模式适合您。请参阅regex demo。
想法是找到行长度,除以2得到中间的char,并从得到的值中减去1或2,并在模式的限制量词中使用它代替30
上方。
模式详情
.{30,}?
- 除了换行符之外的任何30个字符或更多字符,但尽可能少,\K
- 匹配重置运算符,省略了到目前为止匹配的文本\.
- 一个点(?=.{30,})
- 一个积极的前瞻,要求除了当前位置右侧的换行符之外至少存在30个任何字符。