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]---取字符串的多少字节到多少字节
    
     
    
     
  • 相关阅读:
    微信小程序学习随笔
    SqlServer索引假脱机的解决
    web服务器出现大量CLOSE_WAIT连接的前因后果
    SqlServer和mysql字段拼接方法
    使用beego创建员工加班调休系统
    在c#程序中初步使用redis
    使用golang实现批量发送面试邀请邮件
    记c# rabbitmq的使用
    项目中使用mongodb的尝试
    手机集成支付宝支付功能的注意事项
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/2869906.html
Copyright © 2011-2022 走看看