Help with regexp to extract data

1 visualización (últimos 30 días)
Pete sherer
Pete sherer el 3 de Ag. de 2021
Comentada: Pete sherer el 3 de Ag. de 2021
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,

Respuesta aceptada

Stephen23
Stephen23 el 3 de Ag. de 2021
Editada: Stephen23 el 3 de Ag. de 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);

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by