我正在閱讀關於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
,所以我打算通過這種類型的表達式循環遍歷同時跟蹤我是否仍然在標籤(即,是否我通過一個'>'
字符)。這樣做太難了嗎?
感謝您爲我提供的任何指導。