在boost.regex中有没有方便的方式在ascii和utf之间切换?
我现在看到的唯一方法是,例如,在 boost::u32regex
和 boost::regex
之间切换。
这是在 unicode 和 ascii 之间切换的唯一方法吗?
我希望能够只传递一个参数来 boost ,指定我的字符编码,这样就不必重复很多代码。
最佳答案
Is this the only way to switch between unicode and ascii?
差不多。你认为的 boost::regex
实际上是一个类型别名:
namespace boost{
template <class charT, class traits = regex_traits<charT> >
class basic_regex;
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
}
请注意,字符类型是模板参数 - 它不是运行时参数。自 boost::regex
建立在 char
上,它不支持 unicode。
boost::u32regex
也是一样的:
typedef basic_regex<UChar32,icu_regex_traits> u32regex;
为了真正在它们之间进行概括,您也必须将所有内容都写成模板。而不是服用 boost::regex
, 你拿一个 boost::basic_regex<charT, traits>
.这是模板的缺点之一 - 它们几乎渗透到所有内容中。
关于c++ - boost 正则表达式 :switching between ascii and unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37076856/