首页 > 代码库 > 数学之美上的正则表达式 匹配
数学之美上的正则表达式 匹配
#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(){}
数学之美上的正则表达式 匹配
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。