필터 지우기
필터 지우기

Comparing strings with wildcards

조회 수: 215 (최근 30일)
James
James 2013년 5월 26일
댓글: Flurin Arner 2018년 4월 25일
Hi,
I have a number of strings that I'd like to check for a certain ending however the middle of the strings are all different (the middle of the strings are dates). They are of the format: wrfout****_PLEV or wrfout**** where ** is the date (all dates are of the same length). I basically want to find the strings that do not have _PLEV at the end.
I've tried using strcmp with wildcards but for some reason the creators decided it was a fantastic idea to ignore wildcards altogether which in my opinion negates a lot of potential uses of the strcmp function.
I've also tried using strncmp but that only allows you to specify the first n letters of a string to compare and not the last.
I then tried using simple inequalities, to obtain exactly which characters are the same, such as: 'wrfout****_PLEV == wrfout**** but as expected the different lengths of the strings prevented me from using this idea.
Any ideas as to how to do this or how to compare strings with wildcards would be very useful?
Thanks in advance James

채택된 답변

Joao Henriques
Joao Henriques 2014년 5월 15일
There's a very simple way, just use regexptranslate:
regexp('abc123x', regexptranslate('abc*x'))
Will return 1 (beggining of matched string). If there's no match, regexp returns empty.
  댓글 수: 2
Nick Powell
Nick Powell 2014년 10월 23일
Brilliant! Thanks
Flurin Arner
Flurin Arner 2018년 4월 25일
I wanted to add, that regexptranslate will throw an error 'MATLAB:minrhs Not enough input arguments.' like this.
You have to add 'wildcard', to regexptranslate() for it to work:
regexp('abc123x', regexptranslate('wildcard', 'abc*x'))

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

추가 답변 (1개)

the cyclist
the cyclist 2013년 5월 26일
Use the regexp() function.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by