如何正则表达式 PHP 在字符串中查找数字

How to regexp PHP looking for numbers in string

我在使用正则表达式时遇到了问题,我正在寻找这个问题的解决方案。

我需要在文本中查找超过 3 位数字的数字,但是当数字在 beginning/last $ 字符上时我需要省略。

示例:

Lorem ipsum dolor 32 sit 32181527 amet, consectetur adipiscing $18312.90, 2432417$.

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

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

[0-9]{3,}

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

$text = "Lorem ipsum dolor 32 sit 32181527 amet, consectetur adipiscing 312.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
        )

)