#include <stdio.h>
int diff_str(const char *a, const char *b)
{
const char *s = a;
const char *t = b;
for (;; s++, t++)
{
if (*s == NULL && *t == NULL)
return 1;
else if (*s == NULL || *t == NULL)
return 0;
if (*s == *t)
continue;
if (*s == '*')
{
if (*t == '.')
return 0;
if (diff_str(s, t + 1))
return 1;
else if (*(s + 1) == *t)
{
s++;
if (*s == NULL)
return 1;
}
}
else
return 0;
}
}
void d(const char *a, const char *b)
{
int r = diff_str(a, b);
if (r)
printf("'%s' matches '%s'\n", a, b);
else
printf("'%s' DOES NOT match '%s'\n", a, b);
}
int main()
{
d("", "");
d("*", "");
d("*", "t");
d("t", "t");
d("*t", "st");
d("*t", "sat");
d("s*t", "sat");
d("sa*", "sat");
d("s*", "sat");
d("s*t*s", "satas");
d("s*t*s", "sata1s");
d("s*t*s", "sats");
d("s*t*s", "sts");
d("s*t*s", "sat.s");
d("s*t*s", "sata");
d("s*t*s", "sat");
d("s*t*s", "saas");
d("s*s", "satatatas");
d("s*tas", "satatatas");
d("s*tatas", "satatatas");
d("s*tatatas", "satatatas");
d("s*tatatatas", "satatatas");
printf("=====================================\n");
d("au*ex*.bat", "autoexec.bat");
d("au*e*.bat", "autoexec.bat");
d("au*.bat", "autoexec.bat");
d("au**.bat", "autoexec.bat");
d("au*ex*bat", "autoexec.bat");
d("au*ex*.b*t", "autoexec.bat");
d("au*ex*.*", "autoexec.bat");
d("*.*", "autoexec.bat");
printf("=====================================\n");
getchar();
}
结果
'' matches ''
'*' DOES NOT match ''
'*' matches 't'
't' matches 't'
'*t' matches 'st'
'*t' matches 'sat'
's*t' matches 'sat'
'sa*' matches 'sat'
's*' matches 'sat'
's*t*s' matches 'satas'
's*t*s' matches 'sata1s'
's*t*s' matches 'sats'
's*t*s' matches 'sts'
's*t*s' DOES NOT match 'sat.s'
's*t*s' DOES NOT match 'sata'
's*t*s' DOES NOT match 'sat'
's*t*s' DOES NOT match 'saas'
's*s' matches 'satatatas'
's*tas' matches 'satatatas'
's*tatas' matches 'satatatas'
's*tatatas' matches 'satatatas'
's*tatatatas' DOES NOT match 'satatatas'
=====================================
'au*ex*.bat' matches 'autoexec.bat'
'au*e*.bat' matches 'autoexec.bat'
'au*.bat' matches 'autoexec.bat'
'au**.bat' matches 'autoexec.bat'
'au*ex*bat' DOES NOT match 'autoexec.bat'
'au*ex*.b*t' matches 'autoexec.bat'
'au*ex*.*' matches 'autoexec.bat'
'*.*' matches 'autoexec.bat'
=====================================
尚有个小问题 可以代替0个字符么?如果可以的话,那么结果'' DOES NOT match ''有误;如果不可以,则's*t*s' matches 'sts'有误