Regex準系統

來源:互聯網
上載者:User

完美

/*正如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]---取字串的多少位元組到多少位元組  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.