在集成到我的代码之前,我正在为 fnmatch 编写一个基本测试。这是测试:
int main (int argc, char *argv[])
{
for (int i = 2; i < argc; i++)
if (!fnmatch(argv[1], argv[i], 0))
printf("%s matches %s\n", argv[i], argv[1]);
else
printf("%s doesn't matches %s\n", argv[i], argv[1]);
return EXIT_SUCCESS;
}
我将其编译为test
并运行以下命令:
$ ./test a*b? a ab aa acb abc aabc acbc aaaaaaaaaaaaaaab aaaaaaaaaaaaaaaba abb aabb
扩展输出:
a doesn't matches a*b?
ab matches a*b?
aa doesn't matches a*b?
acb matches a*b?
abc matches a*b?
aabc matches a*b?
acbc matches a*b?
aaaaaaaaaaaaaaaba matches a*b?
aaaaaaaaaaaaaaab matches a*b?
abb matches a*b?
aabb matches a*b?
实际输出:
a doesn't matches a*b?
ab doesn't matches a*b?
aa doesn't matches a*b?
acb doesn't matches a*b?
abc matches a*b?
aabc matches a*b?
acbc matches a*b?
aaaaaaaaaaaaaaab doesn't matches a*b?
aaaaaaaaaaaaaaaba matches a*b?
abb matches a*b?
aabb matches a*b?
问题是'?元字符无法匹配 0 个字符(恰好需要一个)。
有谁知道它为什么会这样以及如何解决它?
先感谢您。