簡體   English   中英

使用regex_iterator瀏覽HTML文件的標簽

[英]Using regex_iterator to go through the tags of an HTML file

我正在編寫一個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 to std :: regex_iterator <__ gnu_cxx :: __ normal_iterator,char,std :: regex_traits> :: regex_iterator的main': prog.cpp:(.text.startup+0xd1): undefined reference to (__gnu_cxx :: __ normal_iterator,__gnu_cxx :: __ normal_iterator,std :: basic_regex> const&,std :: bitset <11u>)'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 to未定義引用std::regex_iterator<__gnu_cxx::__normal_iterator<char*, std::string>, char, std::regex_traits<char> >::regex_iterator()' prog.cpp:(.text.startup+0x1af): undefined reference to std :: regex_iterator <__ gnu_cxx的std::regex_iterator<__gnu_cxx::__normal_iterator<char*, std::string>, char, std::regex_traits<char> >::regex_iterator()' prog.cpp:(.text.startup+0x1af): undefined reference to :: __ normal_iterator,char,std :: regex_traits> :: operator!=(std :: regex_iterator <__ gnu_cxx :: __ normal_iterator,char,std :: regex_traits> const&)'prog.cpp :(。text.startup + 0x1be):未定義對`std :: regex_iterator <__ gnu_cxx :: __ normal_iterator,char,std :: regex_traits> :: operator->()'的引用collect2:錯誤:ld返回1退出狀態

知道為什么嗎?

根據這里 ,您在調用中不需要<std::string::iterator> ,您需要使用std :: sregex_iterator(注意s)將正則表達式與std :: string一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM