C++ boost::asio::async_read_,直到带有正则表达式和超时-奇怪的行为

C++ boost::asio::async_read_,直到带有正则表达式和超时-奇怪的行为,c++,regex,timeout,boost-asio,C++,Regex,Timeout,Boost Asio,基于Boost示例()中的代码 我创建了一个函数read_expect(),如下所示: std::string TcpClient::read_expect(const boost::regex & expected, boost::posix_time::time_duration timeout) { // Set a deadline for the asynchronous operation. Sin

基于Boost示例()中的代码 我创建了一个函数read_expect(),如下所示:

std::string TcpClient::read_expect(const boost::regex & expected,
                                   boost::posix_time::time_duration timeout)
{
    // Set a deadline for the asynchronous operation. Since this function uses
    // a composed operation (async_read_until), the deadline applies to the
    // entire operation, rather than individual reads from the socket.
    deadline_.expires_from_now(timeout);

    // Set up the variable that receives the result of the asynchronous
    // operation. The error code is set to would_block to signal that the
    // operation is incomplete. Asio guarantees that its asynchronous
    // operations will never fail with would_block, so any other value in
    // ec indicates completion.
    boost::system::error_code ec = boost::asio::error::would_block;

    // Start the asynchronous operation itself. The boost::lambda function
    // object is used as a callback and will update the ec variable when the
    // operation completes. The blocking_udp_client.cpp example shows how you
    // can use boost::bind rather than boost::lambda.
    boost::asio::async_read_until(socket_, input_buffer_, expected, var(ec) = _1);

    // Block until the asynchronous operation has completed.
    do
    {
        io_service_.run_one();
    }
    while (ec == boost::asio::error::would_block);

    if (ec)
    {
        throw boost::system::system_error(ec);
    }

    // take the whole response
    std::string result;
    std::string line;
    std::istream is(&input_buffer_);
    while (is)
    {
        std::getline(is, line);
        result += line;
    }

    return result;
}
当我将它与正则表达式一起使用时,它可以很好地工作

boost::regex(".*#ss#([^#]+)#.*")
,但当我将正则表达式更改为

boost::regex(".*#ss#(<Stats>[^#]+</Stats>)#.*")
boost::regex(“.*ss.[^.]+).*
,导致没有超时,导致线程挂起async_read_until()调用。 也许这是一些我在正则表达式中看不到的愚蠢错误,我可以使用第一个版本,但我真的想知道为什么会发生这种情况

谢谢你的洞察力,
Marleen在发布这个问题时出错了:有问题的正则表达式应该是boost::regex(“.*#ss#”([^#]+)#*”),您必须转义
中的
/
字符。正确的表达式:
boost::regex(.*.[^.]ss.+).*.
似乎regex对于异步读取是非常奇怪的分隔符。。但是,我认为这是一个简单的格式错误,可能是关于案例敏感性。