How can I find consecutive digits seperated by spaces?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
jack walker
el 9 de Ag. de 2019
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 comentarios
Respuesta aceptada
Stephen23
el 9 de Ag. de 2019
Editada: Stephen23
el 9 de Ag. de 2019
>> 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 comentarios
Stephen23
el 9 de Ag. de 2019
Editada: Stephen23
el 9 de Ag. de 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.
Más respuestas (1)
Alex Mcaulley
el 9 de Ag. de 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
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!