boost的正则表达式错误
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([^=]+)=([^&]+)&?)+
相关文章:
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- std::正则表达式捕获组语法错误
- 带有正则表达式的 c++ 中的错误
- 为什么这个正则表达式在 STL 世界中是错误的?
- C++抛出错误"Invalid special open parenthesis."中的正则表达式
- 正则表达式分段错误
- 使用boost正则表达式时出现shared_ptr错误
- 正则表达式中带有卷轴大括号的正则表达式错误
- 提升正则表达式错误:未初始化的提升::match_results
- C++ 正则表达式错误(未处理的异常)
- 提升正则表达式中的链接器错误
- Ubuntu 上的正则表达式错误
- C++正则表达式错误
- Mingw32-w64 GCC 4.9.2 正则表达式错误,或者我不知道C++
- 函数正则表达式C++在 linux(CentOS) 中遇到错误,但在 OSX 中
- 在 C++14 模式下为 libstdc++ 编译 clang 正则表达式程序会导致错误
- boost的正则表达式错误
- 在c++中使用正则表达式时出现错误
- 提升::正则表达式在长表达式不匹配时抛出错误