How can I find consecutive digits seperated by spaces?

4 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2019-8-9
编辑:Stephen23 2019-8-9
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 个评论
jack walker
jack walker 2019-8-9
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 2019-8-9
编辑:Stephen23 2019-8-9
"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.

请先登录,再进行评论。

更多回答(1 个)

Alex Mcaulley
Alex Mcaulley 2019-8-9
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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by