zoukankan      html  css  js  c++  java
  • 正则表达式基本功能

    完美

    /*
    正如BC书中所言可以匹配以下模型:
    c 匹配任意字母c
    . 匹配任意单个字符
    ^ 匹配输入字符串开头
    $ 匹配输入字符串结尾
    * 匹配前一个字符的零个或者多个出现
    */
    #include<stdio.h>
    #define WORDMAX 100
    int 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]---取字符串的多少字节到多少字节
    
     
    
     
  • 相关阅读:
    is 和 == 的区别,utf和gbk的转换,join用法
    python字典的整理信息
    poj分类
    cloud computing
    POJ1007 关于STL排序方法 动态数组的创建 和向量的使用
    math类
    研究生考试感想
    4.11
    重看设计模式 观察者模式
    子串计算 2010北京大学复试机试题
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/2869906.html
Copyright © 2011-2022 走看看