Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c正则表达式找不到字符串_C_Regex - Fatal编程技术网

c正则表达式找不到字符串

c正则表达式找不到字符串,c,regex,C,Regex,我试图用C语言创建一个正则表达式集合,但没有多大成功 目前,我正在尝试查找包含以下正则表达式的include语句: (#include <.+>)|(#include \".+\") 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <regex.h> char *regex_str = "(#include <.+>)|(#include \".+\")"; char

我试图用C语言创建一个正则表达式集合,但没有多大成功

目前,我正在尝试查找包含以下正则表达式的include语句:

(#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(&regex, regex_str, 0);

    if (reti) {
        printf("Could not compile regex.\n");
        exit(1);
    }

    /* Exec Regex */
    reti = regexec(&regex, str, 0, NULL, 0);

    if (!reti) {
        printf("Match\n");
    } else if (reti == REG_NOMATCH) {
        printf("No Match\n");
    } else {
        regerror(reti, &regex, 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(&regex);

    return 0;
}
我得到的结果是:没有对手


我做错了什么?

您可能需要退出您的比赛小组:

char *regex_str = "\\(#include [\"<].*[\">]\\)";

这很可能是一种模式。

事实上,它起作用了:那么我应该转义哪个字符呢?由括号定义的组需要转义,例如\ \\\