简体   繁体   English

使用regex_iterator浏览HTML文件的标签

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

I'm writing a web browser and trying to use regex_iterator to go through the tags of an HTML document and ultimately create a document tree. 我正在编写一个Web浏览器,并尝试使用regex_iterator浏览HTML文档的标签并最终创建文档树。 First I need a regular expression that will get me an HTML tag. 首先,我需要一个可以获取HTML标签的正则表达式。 The following should print out each HTML tag 以下应打印出每个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;
}

if [someRegularExpression] is equal to a regular expression for an HTML tag. 如果[someRegularExpression]等于HTML标签的正则表达式。 Bur I'm getting the following error when I try to run the program: Bur,我尝试运行该程序时遇到以下错误:

/home/svzQOJ/ccEMKoqM.o: In function main': prog.cpp:(.text.startup+0xd1): undefined reference to std::regex_iterator<__gnu_cxx::__normal_iterator, char, std::regex_traits >::regex_iterator(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, std::basic_regex > const&, std::bitset<11u>)' prog.cpp:(.text.startup+0xdc): 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::__normal_iterator, char, std::regex_traits >::operator!=(std::regex_iterator<__gnu_cxx::__normal_iterator, char, std::regex_traits > const&)' prog.cpp:(.text.startup+0x1be): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator, char, std::regex_traits >::operator->()' collect2: error: ld returned 1 exit status /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退出状态

Any idea why? 知道为什么吗?

根据这里 ,您在调用中不需要<std::string::iterator> ,您需要使用std :: sregex_iterator(注意s)将正则表达式与std :: string一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM