Регулярное выражение для соответствия формату заголовка http с "g_regex_match_simple" - PullRequest
1 голос
/ 16 февраля 2020

Я пытаюсь сопоставить формат заголовка HTTP в C, используя glib's g_regex_match_simple:

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;
}

Я получаю FALSE из g_regex_match_simple(), хотя "Test: header1" должно быть допустимо.

Что мне не хватает?

Я попытался ответить в Regexp, чтобы сопоставить краткий формат logcat с g_regex_match_simple , но у меня это не сработало.

Идеи

1 Ответ

0 голосов
/ 16 февраля 2020

Следующие скомпилированы с 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 шаблоне. Я удалил sla sh.

Исходная строка static const char header_regex[] = "/([\\w-]+): (\\w+)"; должна быть static const char header_regex[] = "([\\w-]+): (\\w+)";

                                   ^
                                   |
                                no slash here
...