我遇到了正则表达式问题,我正在寻找该问题的解决方案。
我需要在文本数字中查找超过 3 位数字的位置,但当数字位于开头/最后一个 $ 字符时,我需要省略。
示例:
Lorem ipsum dolor 32 坐 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 $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/