finding string in between

1 visualización (últimos 30 días)
sensation
sensation el 18 de Oct. de 2017
Comentada: sensation el 19 de Oct. de 2017
Hi, so I have a cell string with 100 X 1 like:
18WABO1-12345-0X
18WABO2-12345-0N
18WACE3-12345-00
18WACE4-12345-0R
18WAGUG-12345-0G
18WDUER-12345-0N
I would like to find the string sequence that is always between 18W and first - so the result is:
ABO1
AB02
ACE3
ACE4
AGUG
DUER etc
my example of a code:
%
somestring(:)= eic_p;
underscore_indices= strfind(somestring,'18W');
underscore_indices=cell2mat(underscore_indices);
fs_indices = strfind(somestring,'-');
fs_indices=fs_indices';
your_number=cellfun(@(v)v(1),fs_indices);
somestring(:)= somestring';
for i=1:length(fs_indices)
yourNumber= somestring{i}(underscore_indices(i)+2:your_number(i)-1);
%HOW i can save every iteration? thanks
end
in the last for loop somehow I am getting the weird output and can not save all results so I can have all those 205 abbreviations in one variable (yourNumber).
Thanks a lot,

Respuesta aceptada

per isakson
per isakson el 18 de Oct. de 2017
Editada: per isakson el 18 de Oct. de 2017
yourNumber is overwritten in the loop and only the last value is saved. The first step to fix your code is
yourNumber = cell( length(fs_indices), 1 );
for i = 1 : length(fs_indices)
yourNumber{i} = somestring{i}(underscore_indices(i)+2:your_number(i)-1);
end
There are other ways, e.g. with regular expressions
>> str = '18WABO1-12345-0X';
>> regexp( str, '(?<=18W)[^\-]+(?=\-)', 'match' )
ans =
'ABO1'
and
cac = {
'18WABO1-12345-0X'
'18WABO2-12345-0N'
'18WACE3-12345-00'
'18WACE4-12345-0R'
'18WAGUG-12345-0G'
'18WDUER-12345-0N' };
%
out = regexp( cac, '(?<=18W).+?(?=\-)', 'match' );
out = cat( 1, out{:} );
and
>> out
out =
'ABO1'
'ABO2'
'ACE3'
'ACE4'
'AGUG'
'DUER'
and with indexing
>> str = char( cac );
>> str = str( :, 4:7 )
str =
ABO1
ABO2
ACE3
ACE4
AGUG
DUER
>>
  10 comentarios
Stephen23
Stephen23 el 19 de Oct. de 2017
"where I can find those expressions when I should use ? ^ or/and +."
By reading the documentation ten times:
And then read it another ten times. And practice lots.
Regular expressions are powerful and very useful, but they require practice and attention to detail. Study that page I linked to, and the other pages that it links to as well.
sensation
sensation el 19 de Oct. de 2017
Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by