SRC: * and ? pattern matcher
От: Flamer Кипр http://users.livejournal.com/_flamer_/
Дата: 30.08.02 10:10
Оценка: 139 (18) :)
Вот, вырезал из одного из своих ранних постов:


bool PatternMatch(const char* s, const char* mask)
{

const   char* cp=0;
const   char* mp=0;
  for (; *s&&*mask!='*'; mask++,s++) if (*mask!=*s&&*mask!='?') return 0;
  for (;;) {
    if (!*s) { while (*mask=='*') mask++; return !*mask; }
    if (*mask=='*') { if (!*++mask) return 1; mp=mask; cp=s+1; continue; }
    if (*mask==*s||*mask=='?') { mask++, s++; continue; }
    mask=mp; s=cp++;
  }




Тут и '?' и '*' работают... То есть если есть строка "test" и шаблон "*t?st*", то приведенная функция это дело найдет (то есть найдет соответствие строки шаблону)...

Пример использования:

PatternMatch("command.com","*and*") // = true
PatternMatch("RSDN","*SDN*")        // = true
PatternMatch("no match","*t?st*")   // = false
PatternMatch("test","*t?st*")       // = true
PatternMatch("toster","*t?st*")     // = true
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.