1

I'm using gcc 4.6.3 with -std=c++0x

It's the first time I use std::regex. And I can't see my mistake:

  • The input parsed std::string starts with GET /favicon.ico HTTP/1.1 while my regex pattern is GET(.*).
  • The result group 1 is evaluated to T /favicon.ico HTTP/1.1 whereas I expected something more like /favicon.ico HTTP/1.1

I don't understand why does it keep the T ?

Here's my code:

static const std::string strRegExp="GET(.*)";
std::string computeRequestedItem(const std::string& strHttpRequest)
{
    std::cout << "SEARCHING :" << strRegExp << "in :" << strHttpRequest;
    std::string strResult;
    std::match_results<std::string::const_iterator> result;
    static const std::regex pattern(strRegExp); 
    bool bValid = std::regex_match(strHttpRequest, result, pattern);

    if(!bValid)
    {
        std::cout << "error" << std::endl;
        return strResult;   
    }

    strResult = result[1];

std::cout << "Requested item: " << strResult << std::endl;

    return strResult;
}

And here is the output:

SEARCHING :GET(.*)in :GET /favicon.ico HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

-+-+-+-+-+-+-+-+-+-+-+-+-+ 
Requested item: T /favicon.ico HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169

1 Answers1

4

libstdc++ regex support is only partial. Please see here and read 28 Regular Expression. I believe the code you have should work fine. As a test, I changed std to boost and compiled your code with boost::regex and got the following results as you expected:

Requested item: /favicon.ico HTTP/1.1

I would suggest using boost::regex until libstdc++ regex is fully supported.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166