How to split a string of digits into groups of three from right-to-left using only regular expressions?

22 visualizzazioni (ultimi 30 giorni)
While I have been able to accomplish this task using a varitey of mixed functions such as regexp() and fliplr(), I am ultimately stumped when trying to resort to only regexp() and/or regexprep(). For example, I am trying to convert the a string similar to the following:
str = '12345678';
such that the resulting output is:
{'12'} {'345'} {'678'}
I am also trying to accomplish this task without the use of loops of any sort.
  2 Commenti
Walter Roberson
Walter Roberson il 26 Gen 2020
Is it permitted to use more than one call to regexp() or regexprep(), or does it need to be just a single call to one or the other?
Is the "execute" group of regexprep() to be permitted?
Allen
Allen il 27 Gen 2020
Single call was preferred, but multiple are acceptable. Was hoping to stay away from the execute groups since that technically uses other functions. However, since I have no experience with trying to generate code using them, I would not have minded seeing a few examples. ;-)

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 26 Gen 2020
Modificato: Stephen23 il 27 Gen 2020
With one regexp call (uses a lookaround assertion):
>> str = '12345678';
>> regexp(str, '(^\d{1,2}(?=(\d{3})*$)|\d{3})','match')
ans =
'12' '345' '678'
With two regexp calls (splits string into two tokens, may have empty cells in the output):
>> tkn = regexp(str,'^(\d{0,2})((\d{3})*)$','tokens','once');
>> out = [tkn(1),regexp(tkn{2},'\d{3}','match')]
out =
'12' '345' '678'
or with some extra functions to define the regular expression itself:
>> rgx = ['(\d{0,2})',repmat('(\d{3})',1,fix(numel(str)/3))];
>> regexp(str,rgx,'tokens','once')
ans =
'12' '345' '678'
  7 Commenti
Allen
Allen il 27 Gen 2020
The first two work perfectly under the limitations. I have been trying to solve that one for quite some time now, but had not been successful with implementing a lookaround assertion to capture more than the first token. Thanks to you both.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by