MATLAB: How to find consecutive digits seperated by spaces

MATLABregexpregexprepstrings

I am trying to use regexprep to replace a string sequence of numbers seperated by spaces by the quantity of consecutive numbers in the sequence.
For example input: '1 1 1 4 4 6 7 7 7 7' would output: '3 2 1 4'
I thought the best way to do this would be regexprep, but I can't understand how to quantify a repeated group of characters.
I have tried: regexprep('string', '(\d\s?)*' ,${ [num2str(length($0)),'\s'] })_
But ofcourse the wild card (\d) still applies so every digit is matched. Is there a way to fix \w as only one digit (e.g. 1 or 9) once it has found the first part of the match?

Best Answer

Simple solution using a dynamic match expression:
>> str = '1 1 1 4 4 6 7 7 7 7';
>> out = regexprep(str,'(\d)(??( $1)*)','${num2str(1+nnz($0==32))}')
out =
3 2 1 4
You might also like to download my FEX submission, which can help with developing regular expressions:
Currently it does not support regexprep, but I might include this in an update.