gpt4 book ai didi

c++ - Boost regexp - 搜索结果的空终止

转载 作者:行者123 更新时间:2023-11-28 03:58:34 25 4
gpt4 key购买 nike

  boost::regex re;
re = "(\\d+)";
boost::cmatch matches;
if (boost::regex_search("hello 123 world", matches, re))
{
printf("Found %s\n", matches[1]);
}

结果:“发现 123 个世界”。我只想要“123”。这是 null 终止的问题,还是只是误解了 regex_search 的工作原理?

最佳答案

你不能通过 matches[1] (类型为 sub_match<T> 的对象)到 printf 那样。它给出任何有用结果的事实是您不能指望的,因为 printf 需要一个 char 指针。而是使用:

cout << "Found " << matches[1] << endl;

或者如果你想使用 printf:

printf("Found %s\n", matches[1].str().c_str());

您可以使用 matches[1].str() 获取带有结果的 std::string 对象.

关于c++ - Boost regexp - 搜索结果的空终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2141643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com