首页 > 程序开发 > 软件开发 > C语言 >

正则表达式c语言经典实现

2012-12-05

/** test.c** Created on: 2012-12-4* Author: Administrator*/#include <stdio.h>int matchhere(char *,char *);int matchstar(int c,char *regexp,char *text){char *t;for(t = text; *t !...

/*
* test.c
*
* Created on: 2012-12-4
* Author: Administrator
*/
#include <stdio.h>
int matchhere(char *,char *);
int matchstar(int c,char *regexp,char *text){
char *t;
for(t = text; *t != &#39;\0&#39; &&(*t == c || c==&#39;.&#39;);t++);
do{
if(matchhere(regexp,t))return 1;
}while(t-->text);
return 0;
}
int matchhere(char *regexp, char *text) {
if (regexp[0] == &#39;\0&#39;) {
return 1;
}
if (regexp[1] == &#39;*&#39;) {
return matchstar(regexp[0], regexp + 2, text);
}
if (regexp[0] == &#39;$&#39; && regexp[1] == &#39;\0&#39;) {
return *text == &#39;\0&#39;;
}
if((text[0] != &#39;\0&#39;)&&(regexp[0]==&#39;.&#39;|| regexp[0]==text[0])) {
return matchhere(regexp+1,text+1);
}
return 0;
}
int match(char *regexp, char *text) {
if (regexp[0] == &#39;^&#39;)
return matchhere(regexp + 1, text);
do {
if (matchhere(regexp, text)) {
return 1;
}
} while (*text++ != &#39;\0&#39;);
return 0;
}
int main(){
char *regexp = ".*$";
char *text = "123";
if(match(regexp,text)){
puts("ok");
}
return 0;
}

相关文章
最新文章
热点推荐