C++ 增强正则表达式不保存捕获

C++ Boost Regex doesn't save capture

本文关键字:保存 增强 正则表达式 C++      更新时间:2023-10-16

我正试图与一个网页通信,检索它的源代码,然后搜索和存储特定的字符串(名称)内的输出文件。以下是我所做的:

    #include <iostream>
#include <string>
#include <conio.h>
#include <boost/asio.hpp>
#include <boost/regex.hpp>
#include <fstream>
using namespace std;
ofstream o("out.txt");
int main()
{
    boost::asio::ip::tcp::iostream s("www.lolsummoners.com", "http");
    if(!s)
        cout << "Could not connect to http://www.lolsummoners.com/n";
    s  << "GET /ladders/eune HTTP/1.1rn"
       << "Host: www.lolsummoners.comrn"
       << "Accept: */*rn"
       << "Connection: closernrn" ;
       boost::regex rgx("/leagues/.*>(.+\s*)</a></td>");
    for(string line; getline(s, line); )
    {
        boost::smatch matches;
        if(regex_search(line, matches, rgx ) )
        {
            o << matches[0] << 'n';
        }
    }
}

问题是,在我的输出文件中,它不保存捕获,相反,它保存了整个内容:

/leagues/eune/64657">Kenachi</a></td>

我只想保存"Kenachi"没有"

"

matches[0]是整个匹配表达式。

第一个捕获组在matches[1]