我正在阅读关于std::regex_iterator<std::string::iterator>
的文档,因为我试图学习如何使用它来解析HTML标记。该网站给出的例子是根据CPlusPlus.com使用std :: regex_iterator <std :: string :: iterator>
#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
,所以我打算通过这种类型的表达式循环遍历同时跟踪我是否仍然在标签(即,是否我通过一个'>'
字符)。这样做太难了吗?
感谢您为我提供的任何指导。