- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读有关 std::regex_iterator<std::string::iterator>
的文档因为我正在尝试学习如何使用它来解析 HTML 标签。网站给出的例子是
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("this subject has a submarine as a subsequence");
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
std::regex_iterator<std::string::iterator> rit ( s.begin(), s.end(), e );
std::regex_iterator<std::string::iterator> rend;
while (rit!=rend) {
std::cout << rit->str() << std::endl;
++rit;
}
return 0;
}
( http://www.cplusplus.com/reference/regex/regex_iterator/regex_iterator/ )
我有一个问题:如果 rend
从未初始化,那么它是如何在 rit!=rend
中有意义地使用的? ?
另外,我应该使用什么工具来从 HTML 标记中获取属性?我想要做的是使用 "class='class1 class2' id = 'myId' onclick ='myFunction()' >"
这样的字符串并分成两对
( "class"
, "class1 class2"
), ( "id"
, "myId"
), ( "onclick"
, "myFunction()"
)
然后从那里与他们一起工作。我打算使用的正则表达式是
([A-Za-z0-9\\-]+)\\s*=\\s*(['\"])(.*?)\\2
因此我计划遍历该类型的表达式,同时跟踪我是否仍在标记中(即我是否传递了 '>'
字符)。这样做会不会太难?
感谢您为我提供的任何指导。
最佳答案
“如果 rend
从未初始化”是什么意思?显然,std::regex_iterator<I>
有一个默认的构造函数。由于迭代只是前向迭代,所以结束迭代器只需要是适合检测结束被使用的东西。默认构造函数可以设置rend
相应地。
这是标准 C++ 库中其他几个地方使用的习语,例如 std::istream_iterator<T>
.理想情况下,可以使用不同的类型来指示结束迭代器(例如,请参阅 Eric Niebler's discussion 关于此问题,链接是四页中的第一页),但标准目前要求在使用算法时这两种类型匹配。
关于使用正则表达式解析 HTML 请引用 this answer .
关于c++ - 根据 CPlusPlus.com 使用 std::regex_iterator<std::string::iterator>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094037/
我只找到了 regex_iterators 被初始化为 的例子 regex_iterator:: 如果我有一个包含一系列字符的类,例如: class fooString { private: de
我想对要跳过文本中某些字符的文本进行正则表达式搜索。此处提出的原始问题:Regular Expression library that maintains state, takes input cha
我试图通过逐行读取文件来从文件中获取一些匹配项。我的代码是这样的: std::regex e("id=\"(.+?)\"|title=\"(.+?)\"|summary=\"(.+?)\"|first
我需要一些帮助来了解如何迭代 boost::sregex_iterator 的搜索结果。基本上我传递了一个';'从命令行分隔的一组 IP 地址,我希望能够使用 boost::sregex_iterat
我一直在使用 std::regex_iterator 来解析日志文件。我的程序已经运行了几个星期并且已经解析了数百万行日志,直到今天,当今天我针对日志文件运行它并出现堆栈溢出时。事实证明,只有日志文件
我在使用 Visual Studio 2013,我看到了一个我认为是错误的东西,我希望有人可以确认吗? string foo{ "A\nB\rC\n\r" }; vector bar; for (sr
regex_token_iterator 和 regex_iterator 有区别吗? 看起来他们都做同样的工作,但不确定哪一个性能更好? 最佳答案 两者之间确实存在差异,如果我们查看 cpprefe
我希望能够解决这样的问题:Getting std :: ifstream to handle LF, CR, and CRLF? istream 需要用复杂的定界符标记;这样,标记化 istream
std::string line; 这会抛出 std::runtime_error what(): Memory exhausted: regex_it = boost::sregex_iterat
我正在使用 Boost 来匹配字符串中的子字符串。 Io 遍历结果,我需要使用 regex_iterator() . 那是我找到的唯一用法示例,但我不理解回调。有人可以给我一个函数的用法示例吗? 让我
我可以使用 g++ -c test.cpp -std=c++0x 创建 .o 文件,但无法链接它,出现下一个错误: test.cpp:(.text+0xe5): undefined reference
我一直在寻找一种计算字符串中特定类型的子字符串数量的好方法,比如说,我想计算字符串 'smstyuismsms' 中 'sms' 的出现次数。我在有人建议使用 regex_iterator 的论坛中找
我有几行简单的代码 std::regex_iterator regit (attributesStart, _curIter, _attributeRegex), regend(s
我正在阅读有关 std::regex_iterator 的文档因为我正在尝试学习如何使用它来解析 HTML 标签。网站给出的例子是 #include #include #include int
我是一名优秀的程序员,十分优秀!