part of file name

1 visualización (últimos 30 días)
ayman mounir
ayman mounir el 18 de Jul. de 2019
Comentada: ayman mounir el 19 de Jul. de 2019
If I have a file name such as ' Hello_World_2020 ', I want to take the part between the underscores ' world ', how I would do that.
Regards.

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Jul. de 2019
Editada: Walter Roberson el 18 de Jul. de 2019
s = 'Hello_World_2020';
c = regexp(s, '_', 'split') ;
c{2}
You can also use strsplit() instead of regexp. On the other hand, strsplit() invokes regexp internally.
  3 comentarios
Adam Danz
Adam Danz el 19 de Jul. de 2019
"If I write s=Hello_World_year_2020. the answer would be 'Hallo_year'. what I should modify?"
@ayman, it's not clear what rules you're following.
  • "Hello_World_2020" --> "World": This removes everything before and after the underscores, including the underscores.
  • "Hello_World_year_2020" --> "Hello_year": This doesnt' follow that rule.
I think Walter's solution will come in handy but you'll need to explain the rule you're following.
ayman mounir
ayman mounir el 19 de Jul. de 2019
clear, thanks

Iniciar sesión para comentar.

Más respuestas (3)

Adam Danz
Adam Danz el 18 de Jul. de 2019
s = 'Hello_World_2020';
c = regexp(s,'_(.*)_','tokens');
c = c{1}{1};
c =
'World'
  1 comentario
ayman mounir
ayman mounir el 19 de Jul. de 2019
It is really helpful, thank a lot

Iniciar sesión para comentar.


madhan ravi
madhan ravi el 18 de Jul. de 2019
s=' Hello_World_2020 ';
[~,Wanted]=regexp(s,'_(.*)_','match','tokens');
Wanted{:}
  2 comentarios
ayman mounir
ayman mounir el 19 de Jul. de 2019
Thanks, It is really helpful, woudl mind to explain how the exprsion works. for exmaple If I write s=Hello_World_year_2020. the answer would be 'Hallo_year'. what I should modify?
Regards
madhan ravi
madhan ravi el 19 de Jul. de 2019
As Adam commented , I had the same thoughts too straight away.

Iniciar sesión para comentar.


Jan
Jan el 19 de Jul. de 2019
s = ' Hello_World_2020 '
c = strsplit(s, '_')
c{2}

Categorías

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

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by