我的 php 代码中有一个正则表达式,它应该匹配每个数字和所有“运算符”+-*/^r() 我的正则表达式看起来像这样
/(?:(\d+(?:\.\d+)|(\+)|(-)|(\*)|(\/)|(\^)|(r)|(\()|(\))))/
当使用字符串进行测试时,preg_match_all($expression,"2+2",$results)
它会返回给我
Array
(
[0] => Array
(
[0] => '+'
)
[1] => Array
(
[0] => ''
)
[2] => Array
(
[0] => '+'
)
[3] => Array
(
[0] => ''
)
[4] => Array
(
[0] => ''
)
[5] => Array
(
[0] => ''
)
[6] => Array
(
[0] => ''
)
[7] => Array
(
[0] => ''
)
[8] => Array
(
[0] => ''
)
[9] => Array
(
[0] => ''
)
)
什么时候(如果它工作正常)我应该得到这个
Array
(
[0] => Array
(
[0] => '2'
[1] => '+'
[2] => '2'
)
[1] => Array
(
[0] => '2'
[1] => ''
[2] => '2'
)
[2] => Array
(
[0] => ''
[1] => '+'
[2] = > ''
)
[3] => Array
(
[0] => ''
[1] => ''
[2] => ''
)
[4] => Array
(
[0] => ''
[1] => ''
[2] => ''
)
...
)
请注意,它为所有运算符返回类似的行为,它似乎完全忽略了数字
请注意,我确实需要单独的捕获组,因此每个结果都有不同的匹配索引