• 每日学习总结20240313


    每日总结

    20240313

    1. 正则表达式

    当使用C语言编写正则表达式的程序时,通常会用到以下四个函数来编译、匹配、释放正则表达式以及处理可能的错误:

    1. int regcomp(regex_t *preg, const char *regex, int cflags)
    2. int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)
    3. void regfree(regex_t *preg)
    4. size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)

    让我们逐一详细介绍这些函数的使用方法:

    1. regcomp()
    • 功能:编译正则表达式。
    • 参数
      • preg:指向 regex_t 类型的指针,用于存储编译后的正则表达式。
      • regex:要编译的正则表达式字符串。
      • cflags:编译标志,可以是 REG_EXTENDED(扩展正则表达式)或 REG_BASIC(基本正则表达式)等,或者它们的组合。
    • 返回值:如果编译成功,则返回0;否则返回非零值,表示编译失败。
    • 示例
      regex_t regex;
      int reti = regcomp(&regex, "pattern", REG_EXTENDED);
      if (reti) {
          fprintf(stderr, "Could not compile regex\n");
          exit(EXIT_FAILURE);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    2. regexec()
    • 功能:执行正则表达式匹配。
    • 参数
      • preg:编译后的正则表达式。
      • string:要匹配的字符串。
      • nmatch:匹配数组 pmatch 的大小,即最大匹配数量。
      • pmatch:用于存储匹配位置的数组。
      • eflags:执行标志,通常为0。
    • 返回值:如果匹配成功,则返回0;如果未找到匹配,则返回 REG_NOMATCH;如果发生错误,则返回其他非零值。
    • 示例
      regmatch_t matches[MAX_MATCHES];
      reti = regexec(&regex, source, MAX_MATCHES, matches, 0);
      if (!reti) {
          // 匹配成功
      } else if (reti == REG_NOMATCH) {
          // 未找到匹配
      } else {
          // 匹配时发生错误
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    3. regfree()
    • 功能:释放已编译的正则表达式占用的资源。

    • 参数

      • preg:编译后的正则表达式。
    • 示例

      regfree(&regex);
      
      • 1
    4. regerror()
    • 功能:将错误码转换为相应的错误消息。
    • 参数
      • errcode:错误码,通常是 regcomp()regexec() 返回的非零值。
      • preg:编译后的正则表达式。
      • errbuf:用于存储错误消息的缓冲区。
      • errbuf_size:错误消息缓冲区的大小。
    • 返回值:如果转换成功,则返回实际写入到 errbuf 中的字节数;否则返回0。
    • 示例
      char error_message[100];
      regerror(reti, &regex, error_message, sizeof(error_message));
      fprintf(stderr, "Regex match failed: %s\n", error_message);
      
      • 1
      • 2
      • 3

    以上是使用这四个函数的基本方法。在实际应用中,你可能需要根据具体情况添加更多的错误处理和验证逻辑。

    5. 示例
    #include "regular.h"
    #include "stdlib.h"
    #include "string.h"
    c_Regular::c_Regular(const char *str_regular)
    {
        if(regcomp(®ular_handle,str_regular,0) != 0)
        {
            printf("regular compile error\n");
            exit(0);
        }
        this->match_result = (regmatch_t *)malloc(sizeof(regmatch_t)*MAX_MATCHES);
    }
    
    c_Regular::~c_Regular()
    {
        free(match_result);
        regfree(®ular_handle);
    }
    
    int c_Regular::match(const char *str)
    {
        regmatch_t match;
        int offset = 0;
        int match_count = 0;
    
        while (regexec(®ular_handle,str+offset,1,&match,0) == 0)
        {
            match_result[match_count].rm_so = match.rm_so+offset;
            match_result[match_count].rm_eo = match.rm_eo+offset;
            match_count++;
            offset += match.rm_eo;
        }  
    
        return match_count;
    }
    
    regmatch_t *c_Regular::getMatchResult()
    {
        return match_result;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    #pragma once
    
    #include 
    #include 
    #include 
    
    #define MAX_MATCHES 10
    
    class c_Regular
    {
    private:
        regex_t regular_handle;
        regmatch_t *match_result;
    public:
        c_Regular(const char *str_regular);
        ~c_Regular();
    
        int match(const char *str);
        regmatch_t* getMatchResult();
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    #include 
    #include 
    #include 
    
    #include "regular.h"
    
    int main() {
        char source[] = "Hello, world! This is a sample string in the world";
    
        c_Regular m_regular("world");
    
        int match_num = m_regular.match(source);
        if(match_num)
        {
            printf("match success %d\n",match_num);
            for (size_t i = 0; i < match_num; i++)
            {
                printf("match result start: %d\n", m_regular.getMatchResult()[i].rm_so);
                printf("match result end: %d\n", m_regular.getMatchResult()[i].rm_eo);
            }
            
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    基于Kubesphere容器云平台物联网云平台Devops实践
    如何再kali中下载iwebsec靶场
    简单的小调度器
    WebDAV之π-Disk派盘 + 书藏家
    数据驱动智能护理:看AI如何塑造医疗领域,为灰暗夕阳带来新的曙光
    干货|以产品要素设计解读线上小微信贷
    神经网络算法入门书籍,神经网络相关书籍
    Go sync.Map探究
    在MacBook上实现免费的PDF文件编辑
    redis集群系列四
  • 原文地址:https://blog.csdn.net/weixin_43739167/article/details/136686686