Extracting pieces of string data

I have several hundred files where I need to take specific sections of a string from 3 different formats:
Here are the 3 formats:
A='2007011101_00001_00012_011914.new_1_I_CE_TH_J2.tif';
B='w2c1_00104_3_I_CE_TE_J2.tif';
C='center_20110_2_I_CE_FI_J.tif';
I need to save the part of the string before the (_1_) or (_2_) or (_3_)
So for A, I need: '2007011101_00001_00012_011914.new'
for B, I need: 'w2c1_00104'
for C, I need; 'center_20110'
Right now I am working on a solution using regexp, something along the lines of:
name=char(regexp(char(regexp(A,'\w+_1_','match')),'\w+','match'))
I haven't been able to come up with a way to identify and keep the multiple underscores and alphanumeric data that precede the (_1_)
Maybe I need to separate this functionality into 3 if statements to deal with the 3 different formats I am looking at?
Thank you for your help, Matlab Central is awesome!

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 14 de Ag. de 2014
Editada: Andrei Bobrov el 14 de Ag. de 2014
out = regexp({A,B,C},'.*(?=_\d_I_CE)','match');
out = [out{:}]';

2 comentarios

Will
Will el 14 de Ag. de 2014
Wow, not even 15 minutes later...love it, ya'll are the best!
Will
Will el 14 de Ag. de 2014
Now if I want to identify the (J2) before the .tif at the end of A, how would I modify the expression inside regexp to cut off everything in the beginning up through the last underscore so I can use \w to catch the J and the 2 without taking the (new_1_I_CE_TH_) as well
This gets me that whole chunk:
im_type=char(regexp(char(regexp(A,'(\w*+.tif)','match')),'\w*','match'))

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Ag. de 2014

Comentada:

el 14 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by