C++ 带boost的正则表达式错误

C++ 带boost的正则表达式错误,c++,regex,boost,C++,Regex,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-conta

我正在尝试匹配一个字符串,它看起来像:

/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;
}
如果我把输入字符串改为

std::string input("/new-contact?id=5&");
我明白了

我想我只得到最后一个标记,但我想用最后一个“+”得到所有东西

我错过了什么

它现在正在与:

^/new-contact\\?((([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)&?)+)$

令牌[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;
}
#包括
#包括
#包括
#包括
使用名称空间boost;
正则表达式(([0-9]+)(\\-| |$)(.*);
//进程(ftp):
//成功时返回ftp响应代码,并填充
//带有ftp响应消息的消息。
int process_ftp(常量字符*响应,标准::字符串*消息)
{
cmatch什么;
if(正则表达式匹配(响应、内容、表达式))
{
//什么[0]包含整个字符串
//[1]中包含的响应代码是什么
//什么[2]包含分隔符字符
//[3]包含文本消息的内容。
如果(味精)
msg->assign(什么[3]。第一,什么[3]。第二);
返回std::atoi(what[1]。first);
}
//失败并不匹配
如果(味精)
msg->erase();
返回-1;
}

令牌[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;
}
#包括
#包括
#包括
#包括
使用名称空间boost;
正则表达式(([0-9]+)(\\-| |$)(.*);
//进程(ftp):
//成功时返回ftp响应代码,并填充
//带有ftp响应消息的消息。
int process_ftp(常量字符*响应,标准::字符串*消息)
{
cmatch什么;
if(正则表达式匹配(响应、内容、表达式))
{
//什么[0]包含整个字符串
//[1]中包含的响应代码是什么
//什么[2]包含分隔符字符
//[3]包含文本消息的内容。
如果(味精)
msg->assign(什么[3]。第一,什么[3]。第二);
返回std::atoi(what[1]。first);
}
//失败并不匹配
如果(味精)
msg->erase();
返回-1;
}

我认为正则表达式是解析URL路径的错误工具。我可以推荐一个吗?

我建议正则表达式是解析URL路径的错误工具。我可以推荐一个吗?

您可以尝试使用连续转义
\G

^/new-contact\\?|(?>\\G([^=]+)=([^&]+)&?)+

您可以尝试使用连续转义
\G

^/new-contact\\?|(?>\\G([^=]+)=([^&]+)&?)+

你试过regex_迭代器吗?这只是程序的一小部分,这就是为什么我需要regex_匹配你试过regex_迭代器吗?这只是程序的一小部分,这就是为什么我需要regex_匹配我正试图避免使用其他库(我会在std之后更改boost)我正试图避免使用其他库(我会在std之后更改boost)非常感谢。我错过了带括号的部分,谢谢。我错过了带括号的部分。