How can I find consecutive digits seperated by spaces?

2 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 9 Aug 2019
Edited: Stephen23 on 9 Aug 2019
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.
  2 Comments
jack walker
jack walker on 9 Aug 2019
Thank you this works and is quite simple. I'm hoping to expand my understanding of Regexp as it so useful. Can you please explain how the space before the token $1 works?
Stephen23
Stephen23 on 9 Aug 2019
Edited: Stephen23 on 9 Aug 2019
"Can you please explain how the space before the token $1 works?"
The dynamic match expression matches substrings like this:
'X X X X' % repeated digit X, separated by space character.
%^ matched by 1st group
% ^^^^^^ matched by 2nd group
The 1st group simply matches one digit. The 2nd group is a dynamic match expression, which replaces the $1 with the contents of the 1st group (i.e. the matched digit) and then inserts this back into the regular expression before continuing to parse the string. So this:
'(\d)(??( $1)*)'
is basically like this, for whatever matched digit X:
'(X)( X)*'
which, due to the * operator, is equivalent to zero or more repititions of ' X':
'(X)( X)( X)( X)...' % repeated as many times as it continues to match.
which corresponds to the substring format you specified in your question.

Sign in to comment.

More Answers (1)

Alex Mcaulley
Alex Mcaulley on 9 Aug 2019
An alternative without regexp:
a = '1 1 1 4 4 6 7 7 7 7';
b = str2num(a);
d = [true, diff(b) ~= 0, true];
n = diff(find(d));
out = num2str(n)
out =
3 2 1 4

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!