C++ boost/regex regex_search

C++ boost/regex regex_search

本文关键字:regex search boost C++      更新时间:2023-10-16

考虑以下字符串内容:

string content = "{'name':'Fantastic gloves','description':'Theese gloves will fit any time period.','current':{'trend':'high','price':'47.1000'}";

我从来没有使用过regex_search,我一直在寻找使用它的方法——我仍然不太懂它。从这个随机字符串(它来自一个API)我怎么能抓住两个东西:

1)价格-在本例中为47.1000

2)名称-在本例中神奇的手套

从我所读到的,regex_search将是这里最好的方法。我计划使用价格作为整数值,我将使用regex_replace来删除""。"在转换字符串之前。我只使用regex_replace,我发现它很容易使用,我不知道为什么我在regex_search中挣扎这么多。

主题演讲:

  1. 内容包含在' '
  2. 内容id和值以分隔:
  3. 内容/值用分隔,
  4. id的名称价格的值将有所不同。

我的第一个想法是找到例如price,然后向前移动3个字符(':')并收集所有内容,直到下一个' -但是我不确定我是否完全偏离了轨道。

不需要boost::regex。正则表达式用于更一般的模式匹配,而您的示例非常具体。处理此问题的一种方法是将字符串分解为单独的令牌。下面是一个使用boost::tokenizer:

的示例
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
#include <map>
int main()
{
    std::map<std::string, std::string> m;
    std::string content = "{'name':'Fantastic gloves','description':'Theese gloves will fit any time period.','current':{'trend':'high','price':'47.1000'}";
    boost::char_separator<char> sep("{},':");
    boost::tokenizer<boost::char_separator<char>> tokenizer(content, sep);
    std::string id;
    for (auto tok = tokenizer.begin(); tok != tokenizer.end(); ++tok)
    {
        // Since "current" is a special case I added code to handle that
        if (*tok != "current")
        {
            id = *tok++;
            m[id] = *tok;
        }
        else
        {
            id = *++tok;
            m[id] = *++tok; // trend
            id = *++tok;
            m[id] = *++tok; // price
        }
    }
    std::cout << "Name: " << m["name"] << std::endl;
    std::cout << "Price: " << m["price"] << std::endl;
}

由于您试图解析的字符串似乎是JSON (JavaScript对象表示法),请考虑使用专门的JSON解析器

您可以在http://json.org/上找到多种语言(包括c++)的JSON解析器的综合列表。此外,我还发现了一个关于几种JSON解析器在c++中的优点的讨论,以回答这个SO问题。