How can I find consecutive digits seperated by spaces?
2 ビュー (過去 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?
0 件のコメント
採用された回答
Stephen23
2019 年 8 月 9 日
編集済み: Stephen23
2019 年 8 月 9 日
>> 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 件のコメント
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!