gpt4 book ai didi

c++ - 根据 CPlusPlus.com 使用 std::regex_iterator

转载 作者:行者123 更新时间:2023-11-30 01:50:44 29 4
gpt4 key购买 nike

我正在阅读有关 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com