堆垛机!
我已经尝试解决这个问题有一段时间了,但没有成功。
(.*?(?:\.|\?|!))(?: |$)
上面的模式是捕获并打破段落中的所有句子,并以结尾标点符号表示。
示例
- 今天是最伟大的一天。你是最伟大的。
比赛以三分结束
Match {
1.
Today is the greatest.
You are the greatest.
}
但是,当有一个带句点的数字时,我试图让它不被破坏,并且希望看到以下匹配:
Match {
1.Today is the greatest.
You are the greatest.
}
感谢您提前提供的帮助
最佳答案
使用
.*?[.?!](?=(?<!\d\.)\s+|\s*$)
参见regex proof .
说明
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[.?!] any character of: '.', '?', '!'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
关于php - 正则表达式 php 前瞻号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70050785/