Regex C ++:提取标签之间的子串

时间:2015-06-28 20:50:38

标签: c++ regex

我想在两个标签之间提取一些子字符串。 示例:<column r="1"><t b="red"><v>1</v></t></column> 我想得到:<t b="red"><v>1</v></t>

我不想使用boost或其他库。只是来自C ++的标准内容,除了CERN的ROOT lib,还有TRegexp,但我不知道如何使用它......

1 个答案:

答案 0 :(得分:3)

不应该使用正则表达式来尝试匹配html,但是,对于这种特殊情况,你可以这样做:

#include <string>
#include <regex>

// Your string
std::string str = "<column r="1"><t b=\"red\"><v>1</v></t></column>";

// Your regex, in this specific scenario
// Will NOT work for nested <column> tags!
std::regex rgx("<column.*?>(.*?)</column>");
std::smatch match;

// Try to match it
if(std::regex_search(str.begin(), str.end(), match, rgx)) {
  // You can use `match' here to get your substring
};

正如安东所说:don't