c语言中使用正则表达式需借助regex库。1. 首先包含
C语言中使用正则表达式,需要借助regex库。核心在于理解 regcomp、regexec、regfree 这三个函数,以及 regex_t 结构体。别指望像Python那样一行代码搞定,C语言的实现相对底层,需要更多手动操作,但也更灵活。
解决方案
首先,你需要包含
立即学习“C语言免费学习笔记(深入)”;
#include <regex.h> #include <stdio.h> #include <stdlib.h> int main() { regex_t regex; int ret; const char *pattern = "hello[[:space:]]+world"; // 匹配 "hello" 后面跟着一个或多个空格,然后是 "world" const char *text = "hello world"; // 编译正则表达式 ret = regcomp(®ex, pattern, REG_EXTENDED); // REG_EXTENDED 使用扩展正则表达式语法 if (ret) { fprintf(stderr, "Could not compile regex "); exit(1); } // 执行正则表达式匹配 ret = regexec(®ex, text, 0, NULL, 0); // 0, NULL, 0 表示我们不关心匹配的细节,只关心是否匹配 if (!ret) { printf("Match found! "); } else if (ret == REG_NOMATCH) { printf("No match found. "); } else { char errbuf[100]; regerror(ret, ®ex, errbuf, sizeof(errbuf)); fprintf(stderr, "Regex match failed: %s ", errbuf); exit(1); } // 释放正则表达式 regfree(®ex); return 0; }
regcomp 的第二个参数是你的正则表达式字符串,第三个参数是标志位,例如 REG_EXTENDED 表示使用扩展正则表达式语法,REG_ICASE 表示忽略大小写。
接下来,使用 regexec 函数执行匹配。这个函数会在给定的字符串中搜索与编译后的正则表达式匹配的部分。
regexec 的第二个参数是要匹配的字符串。后面的参数用于存储匹配的细节,例如匹配的起始位置和长度。如果我们只关心是否匹配,可以将这些参数设置为 0 和 NULL。
最后,使用 regfree 函数释放 regex_t 结构体占用的内存。这是一个很重要的步骤,否则会导致内存泄漏。
C语言的正则表达式需要对一些特殊字符进行转义。例如,如果你想匹配一个点号(.),你需要使用 .。如果你想匹配一个反斜杠(),你需要使用 \。而且,C语言字符串本身也需要转义,所以要匹配一个反斜杠,你可能需要写成 "\\"。这有点反人类,但没办法,C语言就是这样。
此外,regcomp 函数的标志位可以影响正则表达式的解析方式。例如,REG_EXTENDED 标志位允许你使用更现代的正则表达式语法,例如 + 和 ?。
#include <regex.h> #include <stdio.h> #include <stdlib.h> int main() { regex_t regex; int ret; const char *pattern = "a\\b"; // 匹配 "a" const char *text = "a\b"; ret = regcomp(®ex, pattern, 0); if (ret) { fprintf(stderr, "Could not compile regex "); exit(1); } ret = regexec(®ex, text, 0, NULL, 0); if (!ret) { printf("Match found! "); } else if (ret == REG_NOMATCH) { printf("No match found. "); } else { char errbuf[100]; regerror(ret, ®ex, errbuf, sizeof(errbuf)); fprintf(stderr, "Regex match failed: %s ", errbuf); exit(1); } regfree(®ex); return 0; }
如果你想提取匹配到的子字符串,你需要使用 regexec 函数的第三个和第四个参数。第三个参数是 size_t nmatch,表示你想提取的子字符串的数量。第四个参数是 regmatch_t pmatch[],是一个 regmatch_t 类型的数组,用于存储匹配到的子字符串的起始位置和长度。
regmatch_t 结构体有两个成员:rm_so 和 rm_eo,分别表示子字符串的起始位置和结束位置。
#include <regex.h> #include <stdio.h> #include <stdlib.h> int main() { regex_t regex; int ret; const char *pattern = "hello([[:space:]]+)(world)"; // 匹配 "hello" 后面跟着一个或多个空格,然后是 "world",并捕获空格和 "world" const char *text = "hello world"; regmatch_t pmatch[3]; // 我们要捕获 3 个子字符串:整个匹配、空格、"world" size_t nmatch = 3; ret = regcomp(®ex, pattern, REG_EXTENDED); if (ret) { fprintf(stderr, "Could not compile regex "); exit(1); } ret = regexec(®ex, text, nmatch, pmatch, 0); if (!ret) { printf("Match found! "); for (int i = 0; i < nmatch; i++) { printf("Match %d: start=%ld, end=%ld ", i, (long)pmatch[i].rm_so, (long)pmatch[i].rm_eo); if (pmatch[i].rm_so != -1 && pmatch[i].rm_eo != -1) { char *substring = (char*)malloc(pmatch[i].rm_eo - pmatch[i].rm_so + 1); strncpy(substring, text + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); substring[pmatch[i].rm_eo - pmatch[i].rm_so] = ' '; printf("Substring %d: %s ", i, substring); free(substring); } } } else if (ret == REG_NOMATCH) { printf("No match found. "); } else { char errbuf[100]; regerror(ret, ®ex, errbuf, sizeof(errbuf)); fprintf(stderr, "Regex match failed: %s ", errbuf); exit(1); } regfree(®ex); return 0; }
注意,pmatch[0] 存储的是整个匹配的起始位置和结束位置,pmatch[1] 存储的是第一个捕获组的起始位置和结束位置,以此类推。如果某个捕获组没有匹配到任何内容,rm_so 和 rm_eo 的值都会是 -1。
regcomp 函数如果编译失败,会返回一个非零值。你可以使用 regerror 函数获取错误信息。
#include <regex.h> #include <stdio.h> #include <stdlib.h> int main() { regex_t regex; int ret; const char *pattern = "hello[[:space:]+world"; // 错误的正则表达式:缺少一个 ] ret = regcomp(®ex, pattern, REG_EXTENDED); if (ret) { char errbuf[100]; regerror(ret, ®ex, errbuf, sizeof(errbuf)); fprintf(stderr, "Regex compilation failed: %s ", errbuf); exit(1); } regfree(®ex); return 0; }
regerror 函数的第一个参数是 regcomp 函数的返回值,第二个参数是指向 regex_t 结构体的指针,第三个参数是用于存储错误信息的缓冲区,第四个参数是缓冲区的大小。
正则表达式的性能可能是一个问题,尤其是在处理大量数据时。以下是一些优化正则表达式性能的技巧:
总的来说,C语言的正则表达式库虽然使用起来比较繁琐,但功能强大且灵活。理解 regcomp、regexec、regfree 这三个函数,以及 regex_t 结构体,你就可以在C语言中使用正则表达式来处理各种字符串匹配问题。记住,及时释放内存,避免内存泄漏!
以上就是C语言中正则表达式怎么匹配C语言regex库的函数详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号