【问题标题】:Using regex_iterator to go through the tags of an HTML file使用 regex_iterator 遍历 HTML 文件的标签
【发布时间】:2015-01-25 02:52:12
【问题描述】:

我正在编写一个 Web 浏览器并尝试使用 regex_iterator 来遍历 HTML 文档的标签并最终创建一个文档树。首先,我需要一个正则表达式,它会给我一个 HTML 标记。下面应该打印出每个 HTML 标记

#include <string>
#include <regex>
#include <iostream>

int main()
{

    std::string s("<!DOCTYPE html><head></head><body><div class='container' id='someId'><p>Here's a p tag</p><p>Here's another p tag</p></div></body>");
    std::regex e("[someRegularExpression]");
    std::regex_iterator<std::string::iterator> htmlTagRover ( s.begin(), s.end(), e );
    std::regex_iterator<std::string::iterator> offend;
    while (htmlTagRover != offend)
        std::cout << htmlTagRover->str() << std::endl;

    return 0;
}

如果[someRegularExpression] 等于 HTML 标记的正则表达式。 Bur 我在尝试运行程序时收到以下错误:

/home/svzQOJ/ccEMKoqM.o:在函数main': prog.cpp:(.text.startup+0xd1): undefined reference tostd::regex_iterator<:__normal_iterator char std::regex_traits>::regex_iterator(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator , std::basic_regex > const&, std::bitset)' prog.cpp:(.text.startup+0xdc): 未定义引用std::regex_iterator<__gnu_cxx::__normal_iterator<char*, std::string>, char, std::regex_traits<char> >::regex_iterator()' prog.cpp:(.text.startup+0x1af): undefined reference tostd::regex_iterator<:__normal_iterator char std::regex_traits>::operator!=(std::regex_iterator<: __normal_iterator char std::regex_traits> const&)' prog.cpp:(.text.startup+0x1be): 未定义引用`std::regex_iterator<:__normal_iterator char std::regex_traits>::operator->()' collect2:错误:ld 返回 1 个退出状态

知道为什么吗?

【问题讨论】:

标签: html c++ regex


【解决方案1】:

根据here,您的调用中不需要&lt;std::string::iterator&gt;,您需要使用std::sregex_iterator(注意s)来将正则表达式与std::string一起使用。

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2013-11-07
    • 2019-06-24
    • 2016-03-08
    相关资源
    最近更新 更多