[英]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.