- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下字符串内容:
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) 名称 - 在本例中 Fantastic gloves
据我所知,regex_search 是这里最好的方法。我计划将价格用作整数值,我将使用 regex_replace 以便在转换之前从字符串中删除“.”。我只使用过 regex_replace,我发现它很容易使用,我不知道为什么我对 regex_search 这么费劲。
主题演讲:
我的第一个想法是找到例如 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;
}
关于C++ boost/正则表达式 regex_search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702345/
这里的 regex_search 只选择本程序中最长的子串,因为输出是整个字符串数据。这是默认行为吗? 此外,如果我像这样传递字符串而不首先将其声明为字符串 regex_search("some da
#include #include int main() { std::string s = "{\"|1|\":\"A\",\"|2|\":\"B\",\"|37|\":\"423423
我尝试传递一对迭代器来表示一个字符串序列: #include #include using namespace std; int main(int argc,char
我正在使用 std::regex 并希望找到与某个用户定义的正则表达式字符串匹配的字符串中的最后一个位置。 例如,给定正则表达式 :.* 和字符串“test:55:last”,我想找到“:last”,
这是我的代码: std::string var = "(1,2)"; std::smatch match; std::regex rgx("[0-9]+"); if(std::regex_search
我正在尝试解析一些文本以确定它是否是链接。这是我的代码: smatch m; regex e("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(
我正在关注 Stroustrup 的 C++ 之旅 - 第 7.3.1 节(第 79 页)中的示例。此代码在 VS 2013 Update 3 上编译但在运行时失败: regex pat {R"(\w
我今天花了一些时间尝试编写一些 Ansible play,以便仅在相关命令输出中不存在相应行时才运行命令。经过一些试验和错误后,我得到了一些对我有用的东西,但我不清楚为什么我最初与空字符串的比较不起作
如何在 ansible playbook 中使用正则表达式查找匹配项,其中变量出现在 regex_search 中争论? 以下剧本找不到匹配项...运行时使用:ansible-playbook pla
我目前正在努力使用 regex_search 制作一个有效的剧本,我试图让剧本通过网络命令的标准输出(显示运行 vlan ID),然后通过正则表达式来提取所有接口(interface)标记, 最终该变
我刚刚开始学习如何使用regex进行字符串处理(C++11新功能)。如果以下问题太愚蠢,请原谅我。 目前我应用以下代码来获取所有匹配项的索引: string str = "aaabxxxaab"; r
这个问题在这里已经有了答案: Is gcc 4.8 or earlier buggy about regular expressions? (3 个答案) 关闭 6 年前。 当我使用以下代码时,re
我使用各种正则表达式逐行解析 C 源文件。首先,我读取了一个字符串中文件的所有内容: ifstream file_stream("commented.cpp",ifstream::binary); s
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
考虑以下字符串内容: string content = "{'name':'Fantastic gloves','description':'Theese gloves will fit any ti
这个问题在这里已经有了答案: My regex is matching too much. How do I make it stop? [duplicate] (5 个答案) 关闭 6 年前。 我
我在这方面苦苦挣扎,我已经到了没有取得任何进展的地步,是时候寻求帮助了。我对 boost 库的熟悉程度仅略高于表面。我正在尝试通过一个相当大的字符串进行渐进式扫描。实际上,它是将文件的全部内容读入 s
我正在尝试在 Visual Studio Ultimate 2012 中编译以下代码。它给我一个错误,指出我正在调用模板 regex_search() 不存在的重载。 #include struct
我正在尝试使用正则表达式从字符串中提取版本号。版本号的格式为“D.D.Dc”,其中“D”是数字(可以是一个或多个实例),“c”是可选的字母字符,两侧由空格包围。 我想从中提取的字符串是这样的: FOO
boost::regex re("(abc)(.*?)"); boost::smatch m; std::string str = "abcdlogin"; boost::regex_search(s
我是一名优秀的程序员,十分优秀!