我必须从文本中去除一些 XML 标签并保留它们的值。
例子
text text <tag>tag_value</tag> text text <a href="example.com">example.com</a>
->
text text tag_value text text example.com
到目前为止,我已经使用了 boost_replace,但现在我无法使用该库。
std::string src(text);
std::string fmt ="";
std::string ex = "(<tag attribute=\"(.*?)\">)|(</tag>)|(<a href(.*?)\">)|(</a>)|(<tag>)|(</tag>))";
boost::regex expr(ex);
std::string s2 = boost::regex_replace(src, expr, fmt, boost::match_default | boost::format_all);
我该如何解决这个问题?哪个图书馆可以帮助我做到这一点?谢谢
最佳答案
永远不要使用正则表达式来解析 XML!
参见 RegEx match open tags except XHTML self-contained tags
您需要一个真正的 XML 库,例如 expat 或 libxml2。
关于c++ - 替换而不升压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997059/