/*
Name: 简单正则式匹配
Author: Unimen
Date: 05/10/2011 9:24
*/
#include <iostream>
#include <cstring>
using namespace std;
bool PregMatch(const char *pat, const char *str)
{
const char *s = NULL;
const char *p = NULL;
bool bBreak = false;
bool bStar = false;
do
{
bBreak = false;
for (s=str, p=pat; *s; ++s, ++p)
{
switch (*p)
{
//'?'只匹配一个任意字符
case '?' :
break;
case '*' :
{
bStar = true;
bBreak = true;
str = s;
pat = p;
//正则式匹配完成,成功匹配
if (!*++pat)
return true;
}
break;
default:
{
if (*s != *p)
{
//如果没有*号,匹配失败
if (bStar != true)
{
return false;
}
//如果有*号,向前移动一个进行下一次匹配
str++;
bBreak = true;
}
}
break;
}
if (bBreak == true)
break;
}
if (bBreak == false)
{
if ('*' == *p)
++p;
return (!*p);
}
}while (true);
}
int main()
{
char *str = "C:\\Documents\\Unimen\\test.txt";
char *pat = "*test.txt";
cout<<PregMatch(pat, str)<<endl;
return 0;
}