将 http header 格式与 "g_regex_match_simple" 匹配的正则表达式

Regexp to match http header format with "g_regex_match_simple"

我正在尝试使用 glib 的 g_regex_match_simple:

在 C 中匹配 HTTP header 格式
static const char header_regex[] = "/([\w-]+): (\w+)";

...

const char header[] = "Test: header1";
if (g_regex_match_simple(header_regex, header, 0, 0))
{
    headers[index] = g_strdup(header);
    index++;
}
else
{
    error_setg(errp, "%s is not a valid http header format", header);
    goto cleanup;
}

尽管 "Test: header1" 应该有效,但我从 g_regex_match_simple() 中得到了 FALSE。

我错过了什么?

我试过答案 Regexp to match logcat brief format with g_regex_match_simple 但它对我不起作用。

想法?

以下是用clang -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include youe_file.c -lgobject-2.0 -lglib-2.0编译的。

#include <stdio.h>
#include <glib.h>

static const char header_regex[] = "([\w-]+): (\w+)";


int main()
{
const char header[] = "Test: header1";
if (g_regex_match_simple(header_regex, header, 0, 0)){
 printf("+\n");}
else {
    printf("-\n");
}

return 0;
}

它给出了肯定的匹配。区别在于 regexp 模式。我删除了一个斜杠。

原始字符串static const char header_regex[] = "/([\w-]+): (\w+)"; 需要 static const char header_regex[] = "([\w-]+): (\w+)";

                                   ^
                                   |
                                no slash here