Rob 最小正则表达式C语言实现

/******************************************************************************
* @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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值