php - 如何使用正则表达式 PHP 查找字符串中的数字

标签 php regex string split preg-match-all

我遇到了正则表达式问题,我正在寻找该问题的解决方案。

我需要在文本数字中查找超过 3 位数字的位置,但当数字位于开头/最后一个 $ 字符时,我需要省略。

示例:

Lorem ipsum dolor 3232181527 amet,consectetur adipiscing $18312.902432417$

关于结果,我只想得到这个数字32181527

我知道如何获取数字,但我不能忽略 $ 何时出现。

[0-9]{3,}

最佳答案

使用negative look-behind, negative look-ahead assertions ,您可以限制前面/后面没有特定模式的匹配:

$text = "Lorem ipsum dolor 32 sit 32181527 amet, consectetur adipiscing $18312.90, 2432417$.";
preg_match_all('/(?<![0-9$])[0-9]{3,}(?![0-9$])/', $text, $matches);
// match 3+ digits which is not preceded by digits/$.
// also the digits should not be followed by digits/$.
print_r($matches);

输出:

Array
(
    [0] => Array
        (
            [0] => 32181527
        )

)

关于php - 如何使用正则表达式 PHP 查找字符串中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790426/

相关文章:

java - 将 String 和 Integer 与 equals 进行比较

python - 将大括号更改为 str.format() 中的标识符

php - youtube 视频 IDS 中存在哪些字母

regex - 具有可选 1、2 或 3 个值的简单 IIS 7 正则表达式

php - 传递给 DoctrineDataCollector::__construct() 的参数 1 必须是 Doctrine\Common\Persistence\ManagerRegistry 的实例

javascript - 无法弄清楚正则表达式匹配,我还有多远?

c# - 使用多行和组的正则表达式

c# - 确定字符串的第一个字符是大写还是小写

php - Laravel:View::share() 和 View::composer() 之间的区别

php - MongoDB,errmsg,异常 : couldn't compile code for: _map, PHP,命令,MapReduce