Regex C++:提取标记之间的子字符串
Regex C++: extract substring between tags
我想在两个标签之间提取一些子字符串。示例:<column r="1"><t b="red"><v>1</v></t></column>
我想得到:<t b="red"><v>1</v></t>
我不想使用boost或其他libs。只是C++中的标准内容,除了CERN的ROOT-lib,还有TRegexp,但我不知道如何使用它…
您不应该使用正则表达式来尝试匹配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
};
正如安东上面所说:不要。
相关文章:
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 编辑字符串以包含括号 c++ 之间的变量
- 同一分隔符之间的多个子字符串
- 在.cpp文件之间传递全局字符串变量?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- 两个字符串之间的数学运算
- 在 Rcpp 中的字符串类型之间转换时出错
- 如何排列二进制字符串以最小化它们之间的距离
- 目标是找到两个 c 字符串之间的公共前缀(必须使用特定的函数标头)
- 我需要在C++的两个字符串之间找到共同的前缀
- 打印C++中字符出现之间/之后的所有子字符串
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- C++获取两个分隔符之间的字符串并替换它
- 如何在没有映射的情况下在枚举和字符串之间进行转换?
- 正则表达式匹配字符串之间的数字
- 有序地图字符串搜索与整数搜索之间的时间复杂度
- C++ 复制字符串的指定索引之间的任何子字符串
- 匹配外部单引号之间的字符串