I've written the regexp ^(?:([1-5])|([1-5]) .*|.* ([1-5])|.* ([1-5]) .*)$
to match either standalone digit 1-5, or a digit separated by at least one space form the rest of the string. I've tested it in online services and the result is the digit itself. However, when using 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 )
The [0] element is a full match which is good. I anticipated the [1] element be 5 but it's empty, and there's another empty element. Why these empty elements appear?
If you used the regex at regex101.com, all non-participating (i.e. those that did not match) groups are hidden. You can turn them on in options:
And you will see them:
A quick fix is to use a branch reset (?|...)
instead of a non-capturing group (?:...)
and access the $matches[1]
value:
preg_match('/^(?|([1-5])|([1-5]) .*|.* ([1-5])|.* ([1-5]) .*)$/', 'order 12314124 5', $matches);
print_r($matches[1]); // => 5
See the IDEONE demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With