I wrote regexp ^(?:([1-5])|([1-5]) .*|.* ([1-5])|.* ([1-5]) .*)$ To match either a single digit 1-5, or a digit divided by at least one space, the rest of the line. I tested it in online services, and the result is the figure itself. However, when using the code
preg_match('/^(?:([1-5])|([1-5]) .*|.* ([1-5])|.* ([1-5]) .*)$/', 'order 12314124 5', $matches);
I get this:
Array ( [0] => order 12314124 5 [1] => [2] => [3] => 5 )
Element [0] is a complete match, which is good. I assumed that element [1] is 5, but it is empty, and there is another empty element. Why do these empty elements appear?
source share