/******************************************************************************
* @FileName: regexp.c
* @Brief: 这段代码实现了一个正则表达式适配器,用来处理下面的模型
字符 含义
c 匹配任意的字母c
. (句点) 匹配任意的单字符
^ 匹配输入字符串的开头
& 匹配输入字符串的结尾
* 匹配前一个字符串的零个或者多个出现
这是一个非常有用的匹配器,能够满足工作中95%的问题
* @Author: ZhangFajie
* @CreateDate: 2010-10-26
* @Modify_Man: ZhangFajie
* @ModifyNote: Revised format
* @ModifyDate: 2010-11-27
* @Version: V0.0.3
Copyright (c) 2010, CY. All rights reserved.
******************************************************************************/
///
// Include head file
///
#include <stdio.h> // 引用标准库的头文件
#include "typedef.h" // 引用类型定义文件
///
// Type definition
///
///
// Constant definition
///
///
// Maroc definition
///
///
// Variable declaration
///
///
// Function statement
///
int match(char *regexp,char *text) ;
int matchhere(char *regexp,char *text) ;
int matchstar(int c,char *regexp,char *text) ;
///
// Function definition
///
/******************************************************************************
* @Brief: 在text中查找regexp
* @Function: int match()
* @CreateDate: 2011-01-11
* @Param1:
* @Return:
* @ModifyMan:
* @ModifyBrief:
* @ModifyDate:
******************************************************************************/
int match(char *regexp, char *text)
{
if(regexp[0] == '^')
{
return matchhere(regexp+1, text);
} // end of if()
do{ /*即使字符串为空也必须检查*/
if (matchhere(regexp,text))
return 1;
}while (*text++!= '/0');
return 0;
} // end of match()
/*matchhere在text的开头查找regexp*/
int matchhere(char *regexp,char *text)
{
if (regexp[0] == '/0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0],regexp+2,text);
if (regexp[0] == '$' && regexp[1]=='/0')
return *text == '/0';
if (*text!='/0' && (regexp[0]=='.' || regexp[0]==*text))
return matchhere(regexp+1,text+1);
return 0;
} // end of matchhere()
/*matchstar :在text的开头查找C*regexp*/
int matchstar (int c,char *regexp,char *text)
{
// 通配符*匹配零个或多个实例
do {
if (matchhere(regexp,text))
{
return 1 ;
} // end of if (matchhere(regexp,text))
}while (*text!='/0' && (*text++ ==c || c== '.'));
return 0;
} // end of matchstar()