[英]Regex error with boost
I'm trying to match a string who looks like: 我正在尝试匹配一个看起来像这样的字符串:
/new-contact?id=nb&name=test
or /new-contact?id=nb
/new-contact?id=nb&name=test
或/new-contact?id=nb
Basically the number of arguments is undefined. 基本上,参数的数量是不确定的。
so I have tried this regular expression: 所以我尝试了这个正则表达式:
boost::regex re("^/new-contact\\?(([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+$");
but when I try to use re with the following function: 但是当我尝试通过以下功能使用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;
}
I get 我懂了
output: name=Test
and if I change the input string to 如果我将输入字符串更改为
std::string input("/new-contact?id=5&");
I get 我懂了
output: id=5
I guess I am only getting the last token but I am suppose to get everything with the last "+" ? 我想我只得到最后一个令牌,但我想得到所有带有最后一个“ +”的令牌吗?
What did I miss? 我错过了什么?
It's now working with: 现在可以使用:
^/new-contact\\?((([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+)$
token[0]
is going to contain the entire match. token[0]
将包含整个匹配项。 Subsequent indices give you the sub-tokens of the match, which are determined by the parenthesis in your expression (parenthesized groups are called capturing groups ; use (?:...)
for non-capturing groups). 后续索引为您提供匹配的子标记,该子标记由表达式中的括号确定(括号内的组称为捕获组 ;对于非捕获组 ,使用(?:...)
)。
This is documented here . 这在此处记录 。 Copying the provided example, 复制提供的示例,
#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;
}
I suggest that regular expressions are the wrong tool for parsing URL paths. 我建议正则表达式是解析URL路径的错误工具。 May I suggest an URL parsing library ? 我可以建议一个URL解析库吗?
您可以尝试使用延续转义\\G
:
^/new-contact\\?|(?>\\G([^=]+)=([^&]+)&?)+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.