当使用C语言编写正则表达式的程序时,通常会用到以下四个函数来编译、匹配、释放正则表达式以及处理可能的错误:
int regcomp(regex_t *preg, const char *regex, int cflags)
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)
void regfree(regex_t *preg)
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
让我们逐一详细介绍这些函数的使用方法:
regcomp()
preg
:指向 regex_t
类型的指针,用于存储编译后的正则表达式。regex
:要编译的正则表达式字符串。cflags
:编译标志,可以是 REG_EXTENDED
(扩展正则表达式)或 REG_BASIC
(基本正则表达式)等,或者它们的组合。regex_t regex;
int reti = regcomp(®ex, "pattern", REG_EXTENDED);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(EXIT_FAILURE);
}
regexec()
preg
:编译后的正则表达式。string
:要匹配的字符串。nmatch
:匹配数组 pmatch
的大小,即最大匹配数量。pmatch
:用于存储匹配位置的数组。eflags
:执行标志,通常为0。REG_NOMATCH
;如果发生错误,则返回其他非零值。regmatch_t matches[MAX_MATCHES];
reti = regexec(®ex, source, MAX_MATCHES, matches, 0);
if (!reti) {
// 匹配成功
} else if (reti == REG_NOMATCH) {
// 未找到匹配
} else {
// 匹配时发生错误
}
regfree()
功能:释放已编译的正则表达式占用的资源。
参数:
preg
:编译后的正则表达式。示例:
regfree(®ex);
regerror()
errcode
:错误码,通常是 regcomp()
或 regexec()
返回的非零值。preg
:编译后的正则表达式。errbuf
:用于存储错误消息的缓冲区。errbuf_size
:错误消息缓冲区的大小。errbuf
中的字节数;否则返回0。char error_message[100];
regerror(reti, ®ex, error_message, sizeof(error_message));
fprintf(stderr, "Regex match failed: %s\n", error_message);
以上是使用这四个函数的基本方法。在实际应用中,你可能需要根据具体情况添加更多的错误处理和验证逻辑。
#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;
}
#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();
};
#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;
}