php - 需要帮助在 PHP 中构建正则表达式

标签 php regex

我有以下文本作为示例:

\n\t\t\t\t\t3 comments

谁能帮我构建用于提取数字的 PHP 正则表达式公式? (在本例中 number = 3 )
该号码可以以任何文本作为前缀,但号码后面应该有 <space>接下来是 comments

谢谢。

最佳答案

$text = '\n\t\t\t\t\t3 comments';
if (preg_match('/(\d+)\scomments/', $text, $match)) {
    $number = $match[1];
} else {
    // the number was not found
}

这将匹配字符串中存在的任何数字,并将其分配给变量$number。如果没有找到该号码,可以在else语句中处理。

关于php - 需要帮助在 PHP 中构建正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922866/

相关文章:

php - 如何计算当前日期存储在数据库中的年龄?

php - 从 php json 文件中检索名称 ID

regex - Angular 7 中的模式验证

java - 正则表达式匹配 6 级或更多级别的 URL

php - Laravel 5 Eloquent 关系 : can't modify/overwrite relationship table property

php - 最安全的LAMP加密方法

php - MySQL 是否有 sqlca.sqlcode 或类似代码?

java - 我不知道如何在java中使用这个正则表达式

python regex 简单帮助 - 处理括号

regex - 如何将YouTube的“喜欢和不喜欢”以及比率从YouTube导入到Google表格中?