我正在使用带有 -std=c++0x 的 gcc 4.6.3
这是我第一次使用 std::regex。而且我看不到我的错误:
- 解析的输入 std::string 以
GET /favicon.ico HTTP/1.1
我的正则表达式模式为GET(.*)
. - 结果组 1 被评估为,
T /favicon.ico HTTP/1.1
而我期望更像/favicon.ico HTTP/1.1
我不明白为什么它保留T
?
这是我的代码:
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;
}
这是输出:
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