Help with regexp to extract data

1 次查看(过去 30 天)
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,

采纳的回答

Stephen23
Stephen23 2021-8-3
编辑:Stephen23 2021-8-3
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by