php - 正则表达式 php 前瞻号码

标签 php regex

堆垛机!

我已经尝试解决这个问题有一段时间了,但没有成功。
(.*?(?:\.|\?|!))(?: |$)

上面的模式是捕获并打破段落中的所有句子,并以结尾标点符号表示。
示例

  1. 今天是最伟大的一天。你是最伟大的。

比赛以三分结束

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/

相关文章:

php - 是否可以使用在 URL 中传递的值来查询数据库,并使用 mod_rewrite 将查询结果写入 URL?

php - Magento 产品创建 - 违反完整性约束

javascript - 正则表达式在测试时返回组,但在代码中不返回组?

php - 我应该在 config.xml 文件中放入什么来覆盖 Downloadable(Magento Controller )中的 linkAction?

php - 如果不单独使用,在 if 语句中使用相同的比较运算符 (===) 会失败吗?

regex - 为什么Powershell Regex.Replace会吞下换行符?

javascript - 没有中间表达式重复的正则表达式 (axb|cxd)

r - 为什么 gsubfn 会省略部分匹配项?

php - 检查用户是否登录 laravel 4

regex - 是否可以只用一个正则表达式来解决这个问题?