Help with regexp to extract data

2 ビュー (過去 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);
  1 件のコメント
Pete sherer
Pete sherer 2021 年 8 月 3 日
thanks very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by