При помощи CAtlRegExp вылавливаю URL из HTTP-запроса. В приведенном коде все нормально и на выходе мы получаем такое:
0: "GET"
1: " /foo/bar.html "
2: "HTTP/1.1\r\nHost:
www.google.com.ua\r\n\r\n"
Подскажите, как можно модифицировать регулярное выражение так, чтобы
URL не содержал пустых строк вначале и в конце, т.е вместо
" /foo/bar.html " хотелось бы получить "/foo/bar.html".
CAtlRegExp<> reRequest;
// we should get an URL from request
REParseError status = reRequest.Parse(
"{[a-zA-Z]+}{(.)+}{HTTP/(.)+}"
);
if (REPARSE_ERROR_OK != status)
{
// Unexpected error.
return;
}
CAtlREMatchContext<> mcRequest;
if (!reRequest.Match(
"GET /foo/bar.html HTTP/1.1\r\nHost: www.google.com.ua\r\n\r\n",
//"GET / HTTP/1.1",
&mcRequest))
{
// Unexpected error.
return;
}
for (UINT nGroupIndex = 0; nGroupIndex < mcRequest.m_uNumGroups;
++nGroupIndex)
{
const CAtlREMatchContext<>::RECHAR* szStart = 0;
const CAtlREMatchContext<>::RECHAR* szEnd = 0;
mcRequest.GetMatch(nGroupIndex, &szStart, &szEnd);
ptrdiff_t nLength = szEnd - szStart;
printf_s("%d: \"%.*s\"\n", nGroupIndex, nLength, szStart);
if(nGroupIndex == 1)
{
char* szUrl = (char*)_alloca(nLength+1);
strncpy(szUrl, szStart, nLength);
szUrl[nLength] = '\0';
while(isspace(*szUrl))
szUrl++;
printf_s("%d: \"%s\" \n", nGroupIndex, szUrl);
}
}
_getch();