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

22 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 26 Jan 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 on 27 Jan 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. ;-)

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 26 Jan 2020
Edited: Stephen23 on 27 Jan 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 Comments
Allen
Allen on 27 Jan 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!