完美
/*正如BC書中所言可以匹配以下模型:c 匹配任意字母c. 匹配任意單個字元^ 匹配輸入字串開頭$ 匹配輸入字串結尾* 匹配前一個字元的零個或者多個出現*/#include<stdio.h>#define WORDMAX 100int match(char *regexp,char *text);int matchhere(char*regexp,char*text);int matchstar(int c,char*regexp,char*text);int match(char *regexp,char *text){if (regexp[0] == '^')return matchhere(regexp+1,text);do {if (matchhere(regexp,text))return 1;} while (*text++ != '\0');return 0;}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;}int matchstar(int c,char*regexp,char *text){do {if (matchhere(regexp,text))return 1;} while (*text != '\0' && (*text++ == c || c == '.'));return 0;}int main(char argc,char**argv){printf("%d==match(abc ,abc)\n",match("abc" ,"abc"));printf("%d==match(^a ,abc)\n",match("^a" ,"abc"));printf("%d==match(c$ ,abc)\n",match("c$" ,"abc"));printf("%d==match(a.c ,abc)\n",match("a.c" ,"abc"));printf("%d==match(a.*c,abc)\n",match("a.*c","abc"));printf("-------------------\n");printf("%d==match(ABC ,abc)\n",match("ABC" ,"abc"));printf("%d==match(^B ,abc)\n",match("^B" ,"abc"));printf("%d==match(A$ ,abc)\n",match("A$" ,"abc"));printf("%d==match(a..c,abc)\n",match("a..c","abc"));printf("%d==match(a.*d,abc)\n",match("a.*d","abc"));/*char word[WORDMAX];char *rule = argv[1];char *temp;// while(scanf("%s",word) != EOF) //這裡不能使用scanf,因為它不能處理含有空格的行 也就是不能完整的列印含有搜尋字詞的行 只能列印該行中含有該關鍵詞直到空格 也就是說列印以空格分割的詞語while(temp = fgets(word,100,stdin))//fgets返回NULL當讀到EOF或則錯誤正常返回讀入的儲存指標if (match(rule,word))printf("%s\n",temp);*/return 0;}//[A] ---命令,直接替換//:03 --出現3次,不足之處用0替換//[A0-3]---取字串的多少位元組到多少位元組