part of file name

조회 수: 1 (최근 30일)
ayman mounir
ayman mounir 2019년 7월 18일
댓글: ayman mounir 2019년 7월 19일
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.

채택된 답변

Walter Roberson
Walter Roberson 2019년 7월 18일
편집: Walter Roberson 2019년 7월 18일
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
Adam Danz
Adam Danz 2019년 7월 19일
"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 2019년 7월 19일
clear, thanks

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Adam Danz
Adam Danz 2019년 7월 18일
s = 'Hello_World_2020';
c = regexp(s,'_(.*)_','tokens');
c = c{1}{1};
c =
'World'
  댓글 수: 1
ayman mounir
ayman mounir 2019년 7월 19일
It is really helpful, thank a lot

댓글을 달려면 로그인하십시오.


madhan ravi
madhan ravi 2019년 7월 18일
s=' Hello_World_2020 ';
[~,Wanted]=regexp(s,'_(.*)_','match','tokens');
Wanted{:}
  댓글 수: 2
ayman mounir
ayman mounir 2019년 7월 19일
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 2019년 7월 19일
As Adam commented , I had the same thoughts too straight away.

댓글을 달려면 로그인하십시오.


Jan
Jan 2019년 7월 19일
s = ' Hello_World_2020 '
c = strsplit(s, '_')
c{2}

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by