每日学习总结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);
    }
    
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 {
        // 匹配时发生错误
    }
    
3. regfree()
  • 功能:释放已编译的正则表达式占用的资源。

  • 参数

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

    regfree(&regex);
    
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);
    

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

5. 示例
#include "regular.h"
#include "stdlib.h"
#include "string.h"
c_Regular::c_Regular(const char *str_regular)
{
    if(regcomp(&regular_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(&regular_handle);
}

int c_Regular::match(const char *str)
{
    regmatch_t match;
    int offset = 0;
    int match_count = 0;

    while (regexec(&regular_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;
}

#pragma once

#include <iostream>
#include <string>
#include <regex.h>

#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();
};
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>

#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;
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值