[英]Regex error with boost
我正在尝试匹配一个看起来像这样的字符串:
/new-contact?id=nb&name=test
或/new-contact?id=nb
基本上,参数的数量是不确定的。
所以我尝试了这个正则表达式:
boost::regex re("^/new-contact\\?(([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+$");
但是当我尝试通过以下功能使用re时:
function test()
{
std::string input("/new-contact?id=5&name=Test");
boost:cmatch token;
boost::regex_match(req.c_str(), token, input);
std::cout << token[1] << std::endl;
}
我懂了
output: name=Test
如果我将输入字符串更改为
std::string input("/new-contact?id=5&");
我懂了
output: id=5
我想我只得到最后一个令牌,但我想得到所有带有最后一个“ +”的令牌吗?
我错过了什么?
现在可以使用:
^/new-contact\\?((([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+)$
token[0]
将包含整个匹配项。 后续索引为您提供匹配的子标记,该子标记由表达式中的括号确定(括号内的组称为捕获组 ;对于非捕获组 ,使用(?:...)
)。
这在此处记录 。 复制提供的示例,
#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace boost;
regex expression("([0-9]+)(\\-| |$)(.*)");
// process_ftp:
// on success returns the ftp response code, and fills
// msg with the ftp response message.
int process_ftp(const char* response, std::string* msg)
{
cmatch what;
if(regex_match(response, what, expression))
{
// what[0] contains the whole string
// what[1] contains the response code
// what[2] contains the separator character
// what[3] contains the text message.
if(msg)
msg->assign(what[3].first, what[3].second);
return std::atoi(what[1].first);
}
// failure did not match
if(msg)
msg->erase();
return -1;
}
我建议正则表达式是解析URL路径的错误工具。 我可以建议一个URL解析库吗?
您可以尝试使用延续转义\\G
:
^/new-contact\\?|(?>\\G([^=]+)=([^&]+)&?)+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.