简体   繁体   中英

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. First I need a regular expression that will get me an HTML tag. The following should print out each HTML tag

#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. Bur I'm getting the following error when I try to run the program:

/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

Any idea why?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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