如何在 boost::u32regex & reg 中打印值

How to print value in boost::u32regex & reg

本文关键字:reg 打印 u32regex boost      更新时间:2023-10-16

我想使用std::coutboost::u32regex & reg中打印一个值。

对于boost::regex & reg,我可以打印reg.str(),但不能使用str()boost::u32regex

有人能告诉我吗?

似乎boost::u32regex后面使用的类型与cout不兼容。他们似乎在使用ICU库中的Uchar32

您可以使用迭代器打印regex值:

#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
#include <unicode/ustream.h>
void PrintRegex32( const boost::u32regex& r )
{
    boost::u32regex::iterator it  = r.begin();
    boost::u32regex::iterator ite = r.end();
    for ( ; it != ite; ++it )
    {
        std::cout << UnicodeString(*it) << std::endl;
    }
}

这对我来说很有效。它不像打印boost::regex值那么容易,但它很有效。我建议您创建一个函数来执行此操作,如示例中所示。

编辑:

你可以试试代码:

boost::u32regex r = boost::make_u32regex("(?:\A|.*\)([^\]+)");
PrintRegex32( r );

我可以打印reg.str()

仅供参考,boost::basic_regex有一个operator<<过载,他们正在做完全相同的事情,所以:

// reg is a boost::regex
std::cout << reg.str() << std::endl;

和是一样的

// reg is a boost::regex
std::cout << reg << std::endl;