Reputation: 308
i have a regex in my php code that should match every number and all "operators" +-*/^r() my regex looks like this
/(?:(\d+(?:\.\d+)|(\+)|(-)|(\*)|(\/)|(\^)|(r)|(\()|(\))))/
and when tested with the string preg_match_all($expression,"2+2",$results)
it gives me back
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] => ''
)
)
When (if it works right) i should be getting this
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] => ''
)
...
)
Note, it returns similar behaviour for all of the operators, it seems to ignore the numbers entirely
Note, I DO need separate capture groups so i have different match indexes for each result
Upvotes: 0
Views: 113
Reputation: 89557
you can simply do this:
$pattern = '~\d+(?:\.\d+)?|[-+*/^r()]~';
Upvotes: 1