First, I want to say that the behavior seems a bit weird to me. But it can be understood why the pattern matching in Lua is like that. As for %d*
, Lua tried to match from beginning of your string, and matched a zero-length string.
local str = "foo 123 bar"
local result = str:match('%d*')
print(type(result), #result)
As you can see, it outputs string 0
. It's not nil
, so the matching is successful. That's how Lua interprets your pattern. When it comes to %s%d*
, Lua cannot match a zero-length string, thus goes forward for 123
.
To conclude, Lua won't look for a longer match even if it found a successful match the length of which is zero.