C++ boost/regex regex_search
C++ boost/regex regex_search
考虑以下字符串内容:
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中挣扎这么多。
主题演讲:
- 内容包含在' '
- 内容id和值以分隔:
- 内容/值用分隔,
- 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问题。
相关文章:
- 此模式的C++RegEx
- QTableview Search
- LeetCode 1011.Binary Search,C++和Python的想法相同,但输出不同
- C++ std::regex 使用前瞻失败
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 二叉搜索树 - 实现"search"函数
- 使用 boost::regex 从目录中获取带有一些正则表达式的文件名称时出现意外输出
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- 使用Regex解析cpp中的字符串并创建映射
- C++:如何用split查找单词?Regex
- 在VC2015U3上,std::regex比boost::regex慢得多
- 是否有更有效的方法来生成日期的REGEX字符串
- 在C++中使用带有regex的捕获组
- 将std::regex设置为静态的好主意吗
- 为什么Regex(c++)需要指数时间
- std::regex and dual ABI
- C++11 std::regex后备选项
- 以不同的方式替换每一项,regex c++
- 使用 boost::regex (c++) 比较两个正则表达式
- 如何理解boost::regex的哪一部分未能匹配/search