-1

I want to fetch the value of href in c++, but my code is not giving the desired result

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


int main()
{

   std::regex url("/.*(href=')(.*)('>)/");
  std::string url_test = "hjsh.ppt";
    std::ifstream file("in.txt");
    if (!file.is_open())
    {
        std::cerr << "Failed to open file!\n";
        return -1;
    }


    const std::string needle = "href";


    while (std::getline(file, url_test))
    {
        if (url_test.find(needle) != std::string::npos)
        {
          if(regex_match(url_test, url)){}
            std::cout << url_test << "\n";

        }
    }
}

The above code prints whole line as

<a href="11_custom_io.ppt">Ch11: Customizing I/O</a>

I want only 11_custom_io.ppt , name of the file. Please Help.

lazyborg
  • 41
  • 8
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 –  Apr 01 '17 at 14:24

1 Answers1

0

As already mentioned in comments it is not a good idea to parse XML or HTML using regexes. But if you want to get sub-match then you can use std::match_results. For example:

std::string line("<a href='11_custom_io.ppt'>Ch11: Customizing I/O</a>");
std::regex re("href='(.*)'>");
std::smatch match;

if ( std::regex_search(line, match, re) ) {
    std::cout << "sub-match " << match[1].str() << '\n';
}

And output will be: sub-match 11_custom_io.ppt

JustRufus
  • 492
  • 1
  • 5
  • 10