What's going on
Let's simplify the capturing out of your regex for a while, as it isn't responsible for whats happening. Your regex is thus as such:
/.*? .*? .*?/
The meaning of .*?
is "match any character (except a newline), none to many times, as few as possible."
In this context, the first .*?
would attempt to match zero characters from the string, then fail on the next regex element, the space. It would try again matching one, two... characters from the string, and will first succeed when the next character is an actual space.
Put in another way, it's the fact we've got a space after the .*?
group that makes it match what you want. Else it would just happily stop matching at zero characters.
This is precisely what's going on for your third match. Since your regex ends there, a null match does satisfy the regex group, and is the preferred match.
Ways to avoid it
As the other answers have put it, possible solutions include:
split
(best transcription of intended semantice IMO)
- making the last capture greedy (
.*
instead of .*?
)
- adding something (anything that matches) past the last capture.
$
if the line ends there
- matching on non-spaces (
\S
) instead of any character (.
). This would work with either greedy (\S*
) or nongreedy (\S*?
) matches.