MATLAB: Regexp: \w* vs \w+

MATLABpattern matchingregexp

Given:
t = " the cat";
pStar = "\w*";
pPlus = "\w+";
regexp(t, pStar, 'match'),
regexp(t, pPlus, 'match'),
Both return:
ans =
1×2 string array
"the" "cat"
My question is why \w*, which is supposed to match zero or more occurrences of the \w class, doesn't match the two spaces and return them, either as leading characters in the two substrings (" the" and " cat") or as additional standalone substrings. I guess I'm unclear what "zero occurrences" means here. When the regex engine examines the 1st space, why wouldn't it match it, since it indeed matches zero occurrences of the class? Perhaps someone familiar with the engine's behavior can help.

Best Answer

\w does not match spaces. \s is used to match spaces. \w only matches a-z A-Z 0-9 and underscore. Following attempts to show the difference between * and +
t = " the cat";
pStar = "\w*\s";
pPlus = "\w+\s";
regexp(t, pStar, 'match')
regexp(t, pPlus, 'match')