How can I find consecutive digits seperated by spaces?

조회 수: 2 (최근 30일)
jack walker
jack walker 2019년 8월 9일
편집: Stephen23 2019년 8월 9일
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