0

我必须从文本中删除一些 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);

我怎么能解决这个问题?哪个图书馆可以帮助我做到这一点?谢谢

4

1 回答 1

1

永远不要使用正则表达式来解析 XML!

请参阅RegEx 匹配打开的标签,XHTML 自包含标签除外

您需要一个真正的 XML 库,例如 expat 或 libxml2。

于 2012-12-21T20:56:43.890 回答