如何只split_regex一次
How to split_regex only once?
函数模板boost::algorithm::split_regex
将单个字符串拆分为原始字符串子字符串上的字符串,该字符串与我们传递给split_regex
的正则表达式模式匹配。问题是:如何在匹配的第一个子字符串上只拆分一次?也就是说,split_regex
第一次分裂后是否有可能停止?请参阅以下代码。
#include <boost/algorithm/string/regex.hpp>
#include <boost/format.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <locale>
int main(int argc, char *argv[])
{
using namespace std;
using boost::regex;
locale::global(locale(""));
// Create a standard string for experiment.
string strRequestLine("Host: 192.168.0.1:12345");
regex pat(R"(:s*)", regex::perl | boost::regex_constants::match_stop);
// Try to split the request line.
vector<string> coll;
boost::algorithm::split_regex(coll, strRequestLine, pat);
// Output what we got.
for (const auto& elt : coll)
cout << boost::format("{%s}n") % elt;
// Exit the program.
return 0;
}
应该在哪里修改代码以获得类似输出
{Host}
{192.168.0.1:12345}
而不是电流输出
{Host}
{192.168.0.1}
{12345}
有什么建议/提示吗?谢谢。
请注意,我不是在问如何使用其他函数或模式来做到这一点。我问split_regex
是否有可能只分裂一次然后停止。因为regex
对象似乎有能力在第一个匹配时停止,我想知道如果给它一些适当的标志,它可能会在第一个匹配时停止。
对于您的特定输入,似乎简单的解决方法是更改模式以变得R"(:s+)"
。当然,这假设 Host:
之后至少有一个空格,IP 地址和端口之间没有空格。
另一种选择是不使用split_regex()
而是std::regex_match()
:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string strRequestLine("Host: 192.168.0.1:12345");
std::smatch results;
if (std::regex_match(strRequestLine, results, std::regex(R"(([^:]*):s*(.*))"))) {
for (auto it(++results.begin()), end(results.end()); it != end; ++it) {
std::cout << "{" << *it << "}n";
}
}
}
从我的评论扩展:
您可能对我在此处列出的第一个示例感兴趣:小型 HTTP 响应标头解析函数。摘要:使用
phrase_parse(f, e, token >> ':' >> lexeme[*(char_ - eol)], space, key, value)
下面是一个简单的示例:
住在科里鲁
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
namespace {
typedef std::string::const_iterator It;
// 2.2 Basic Rules (rfc1945)
static const qi::rule<It, std::string()> rfc1945_token = +~qi::char_( " t><@,;:\"/][?=}{:"); // FIXME? should filter CTLs
}
#include <iostream>
int main()
{
std::string const strRequestLine("Host: 192.168.0.1:12345");
std::string::const_iterator f(strRequestLine.begin()), l(strRequestLine.end());
std::string key, value;
if (qi::phrase_parse(f, l, rfc1945_token >> ':' >> qi::lexeme[*(qi::char_ - qi::eol)], qi::space, key, value))
std::cout << "'" << key << "' -> '" << value << "'n";
}
指纹
'Host' -> '192.168.0.1:12345'
相关文章:
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在C++中一次将矢量值写入多个文件
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 在一次迭代中从 txt 文件中读取多行
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 高级选择排序 - 在一次迭代中搜索两个元素
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何仅使用一次固定<<设置精度(2)?或者至少恢复到默认行为?
- 什么是仅调用一次并调用参数的控制台应用
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- (C++)虽然循环一次不起作用,但我引入了多个变量
- 为什么我的信号处理程序只执行一次?
- 如何使用C++一次读取整个二进制文件
- 花括号使循环运行一次?
- 从子菜单返回后,正确的输入至少进入验证循环一次
- 蓝牙插座只能读取一次,为什么?