Help with regexp to extract data

조회 수: 1 (최근 30일)
Pete sherer
Pete sherer 2021년 8월 3일
댓글: Pete sherer 2021년 8월 3일
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