c正则表达式找不到字符串
我试图用C语言创建一个正则表达式集合,但没有多大成功 目前,我正在尝试查找包含以下正则表达式的include语句:c正则表达式找不到字符串,c,regex,C,Regex,我试图用C语言创建一个正则表达式集合,但没有多大成功 目前,我正在尝试查找包含以下正则表达式的include语句: (#include <.+>)|(#include \".+\") 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <regex.h> char *regex_str = "(#include <.+>)|(#include \".+\")"; char
(#include <.+>)|(#include \".+\")
这是我的密码:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
char *regex_str = "(#include <.+>)|(#include \".+\")";
char *str = "#include <stdio.h>";
regex_t regex;
int reti;
int main() {
/* Compile Regex */
reti = regcomp(®ex, regex_str, 0);
if (reti) {
printf("Could not compile regex.\n");
exit(1);
}
/* Exec Regex */
reti = regexec(®ex, str, 0, NULL, 0);
if (!reti) {
printf("Match\n");
} else if (reti == REG_NOMATCH) {
printf("No Match\n");
} else {
regerror(reti, ®ex, str, sizeof(str));
printf("Regex match failed: %s\n", str);
exit(1);
}
/* Free compiled regular expression if you want to use the regex_t again */
regfree(®ex);
return 0;
}
我得到的结果是:没有对手
我做错了什么?您可能需要退出您的比赛小组:
char *regex_str = "\\(#include [\"<].*[\">]\\)";
这很可能是一种模式。事实上,它起作用了:那么我应该转义哪个字符呢?由括号定义的组需要转义,例如\ \\\