Help with regexp to extract data

3 views (last 30 days)
Hi
I need help extracting the text and number or text before and after the "_". When an underscore is not present, then the whole text is considerd the latter part.
tdata= {'W1W','W1W 7','W1W 8','AZ_85262','CA_90032','CA_90045'};
the answer should be
fr = {'','','','AZ','CA','CA'}; % first portion
ed = {'W1W','W1W 7','W1W 8','85262','90032','90045'}; % the end portion
Thanks,

Accepted Answer

Stephen23
Stephen23 on 3 Aug 2021
Edited: Stephen23 on 3 Aug 2021
tdata = {'W1W','W1W 7','W1W 8','AZ_85262','CA_90032','CA_90045'};
tkn = regexp(tdata,'^(.*?)_?([^_]*)$','tokens','once');
tkn = vertcat(tkn{:})
tkn = 6×2 cell array
{0×0 char} {'W1W' } {0×0 char} {'W1W 7'} {0×0 char} {'W1W 8'} {'AZ' } {'85262'} {'CA' } {'90032'} {'CA' } {'90045'}
fr = tkn(:,1);
ed = tkn(:,2);

More Answers (0)

Categories

Find more on Language Support in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!