1

I am trying to parse HTTP headers with POSIX ERE regex library (C GNU Regex library in regex.h, and yes, I know about boost). I am using pattern:

([0-9a-zA-Z-]*):[[:space:]]*(.*)[[:space:]]*\r\n(.*) 

(last group for recursive parse), my headers:

HTTP/1.1 200 OK\r\nServer: nginx/0.9.2\r\nDate: Tue, 15 Jul 2014 15:08:12 GMT\r\nContent-Type: text/plain;charset=UTF-8\r\nConnection: keep-alive\r\n

I have checked this pattern in Regex Buddy 3.1.1 and it matches, but my code somehow returns no matches, although it works normal in other situations. Can anyone help me with this?

    regcomp(&compiledRegEx,pattern.c_str(), compileFlags);
    size_t numberOfSubExpressions = compiledRegEx.re_nsub + 1; 

    matches = new regmatch_t[numberOfSubExpressions];

    auto errorCode = regexec(&compiledRegEx,expression.c_str(),numberOfSubExpressions,matches,REG_EXEC_FLAGS);

    if (errorCode == REG_EXEC_SUCCESS)
    {
        for (size_t i = 0; i < numberOfSubExpressions; ++i)
        {
            // If no matches left - break out
            if (matches[i].rm_so == -1 || matches[i].rm_eo == -1) break;
            // Extracting substring and adding to results   // eo points to the character after the last one
            std::string subString = expression.substr(matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
            results.push_back(subString);
        }
    ...
user3177112
  • 395
  • 1
  • 2
  • 12
  • 1
    Unrelated to your problem, but since you have a C++11 capable compiler (as indicated by the use of `auto` type deduction for `errorCode`), why not use the standarc library C++11 [regex functionality](http://en.cppreference.com/w/cpp/regex)? – Some programmer dude Jul 16 '14 at 07:15
  • @JoachimPileborg One should know that at least GCC4.8 may produce the same flaws using the c++11 standard regex features. – πάντα ῥεῖ Jul 16 '14 at 07:28
  • @Joachim Pileborg It is GCC 4.4.6 (modified probably) so default regex was not implemented yet see http://stackoverflow.com/questions/4716680/c0x-regex-in-gcc – user3177112 Jul 16 '14 at 12:20

0 Answers0