首页 > 其他 > 详细

数学之美上的正则表达式 匹配

时间:2014-11-08 15:04:38      阅读:283      评论:0      收藏:0      [点我收藏+]

 

#include <stdio.h>

/* match: search for regexp anywhere in text */
int match(char *regexp, char *text)
{
    if (regexp[0] == ‘^‘)
        return matchhere(regexp+1, text);
    do   /* must look even if string is empty */
    {
        if (matchhere(regexp, text))
            return 1;
    }
    while (*text++ != ‘\0‘);
    return 0;
}
/* matchhere: search for regexp at beginning of text */
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;
}
/* matchstar: search for c*regexp at beginning of text */
int matchstar(int c, char *regexp, char *text)
{
    do   /* a * matches zero or more instances */
    {
        if (matchhere(regexp, text))
            return 1;
    }
    while (*text != ‘\0‘ && (*text++ == c || c == ‘.‘));
    return 0;
}

int main()
{

}

  

数学之美上的正则表达式 匹配

原文:http://www.cnblogs.com/juandx/p/4083378.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!