C++多个字符串抓取器(正则表达式)
C++ multiple string grabber(regex)
我对 boost::regex 有问题,此解决方案仅适用于每场比赛中的一个结果
boost::regex regex("id="(.*?)""); // should I use this "id="(.*?)"(.*?)<value>(.*?)</value>"?
boost::sregex_token_iterator iter(xml.begin(), xml.end(), regex, 1); // 1 because I just need text inside quotes
boost::sregex_token_iterator end;
现在解析的字符串
<x id="first">
<value>5</value>
</x>
<x id="second">
<value>56</value>
</x>
etc...
现在的问题是如何一次解析 id 和值以在匹配循环中同时抓取它们
for( ; iter != end; ++iter ) {
std::string id(iter->first, iter->second);
std::string value(?????);
}
Boost.PropertyTree 包含一个 XML 解析器,您可以使用它来代替正则表达式:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
...
using boost::property_tree::ptree;
ptree pt;
read_xml(istreamOrFilename, pt);
BOOST_FOREACH(ptree::value_type &v, pt) {
std::string id(v.second.get<std::string>("<xmlattr>.id"));
std::string value(v.second.get<std::string>("value").data());
}
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 组合正则表达式部分使用 | 不起作用的 C++
- 为什么C++正则表达式这么慢?
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- 在C++中实现正则表达式
- C++正则表达式替换整个单词
- 用C++编写正则表达式的正确方法是什么?
- 如何从Stroustrup的C++书中解释这个正则表达式?
- 为什么这个正则表达式C++在括号表达式中抛出无效范围异常?
- C++多个字符串抓取器(正则表达式)
- Qt正则表达式增强正则表达式转换从字符串内部取数