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]---取字符串的多少字节到多少字节
    
     
    
     
  • 相关阅读:
    Redis安装与配置
    Mysql主从复制
    MySQL的基本使用
    如何理解 python 里面的 for 循环
    我在创业公司的 “云原生” 之旅
    linux 安装 kafka
    数据库性能优化
    Shell 中各种括号的作用
    DNS 配置实例-正反解析-主从同步-分离解析
    DHCP 中继转发配置(ensp 实现)
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/2869906.html
Copyright © 2011-2022 走看看