Find string ends with xls or xlsx

조회 수: 2 (최근 30일)
chlor thanks
chlor thanks 2016년 7월 13일
댓글: chlor thanks 2016년 7월 14일
I have a char array of strings (each string is a file name) and I would like to find out the strings that ends with xls or xlsx. I know that strcmp can be used only if to compare the first few characters, which is the opposite of what I intent to do---compare the last few characters.
What should I do in this case? Thank you.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 13일
편집: Azzi Abdelmalek 2016년 7월 13일
str='abc.xlsx xls.m df.xls er.doc sd.xls'
out=regexp(str,'\S+\.xlsx?\>','match')
  댓글 수: 6
Guillaume
Guillaume 2016년 7월 14일
I would use this simpler regex:
regexp(str, '\.xlsx?$', 'match', 'once')
Matches '.xls', followed by an optional 'x', followed by the end of the string.
chlor thanks
chlor thanks 2016년 7월 14일
Thank you both! Especially for the notes Guillaume I really appreciate it!

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

추가 답변 (1개)

Star Strider
Star Strider 2016년 7월 13일
The findstr function may do what you want:
fn = 'filename.xlsx';
xls_pos = findstr(fn,'xls')
xls_pos =
10
So if ‘xls_pos’ (in this example) is not empty, the string contains ‘xls’ or ‘xlsx’.
  댓글 수: 2
chlor thanks
chlor thanks 2016년 7월 13일
편집: chlor thanks 2016년 7월 13일
Thank you for sharing Star!
Star Strider
Star Strider 2016년 7월 13일
My pleasure!

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by