part of file name
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
2019년 7월 19일
"If I write s=Hello_World_year_2020. the answer would be 'Hallo_year'. what I should modify?"
- "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.
추가 답변 (3개)
Adam Danz
2019년 7월 18일
s = 'Hello_World_2020';
c = regexp(s,'_(.*)_','tokens');
c = c{1}{1};
c =
'World'
madhan ravi
2019년 7월 18일
s=' Hello_World_2020 ';
[~,Wanted]=regexp(s,'_(.*)_','match','tokens');
Wanted{:}
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!