std::regex_traits<CharT>::value
来自cppreference.com
<tbody>
</tbody>
int value( CharT ch, int radix ) const; |
(C++11 起) | |
给定当前浸染的本地环境,确定数字 ch 在数值底 radix 中表示的值。std::regex 在处理如 {1} 或 {2,5} 的数量词、如 \1 的回溯引用和十六进制及 Unicode 字符转义时调用此函数。
参数
| ch | - | 可以表示数字的字符 |
| radix | - | 8、10 或 16 之一 |
返回值
若 ch 在当前浸染的本地环境中确实表示对于数值底 radix 合法的数字,则为其数值,错误时为 -1。
示例
运行此代码
#include <iostream>
#include <locale>
#include <map>
#include <regex>
// 这个自定义的正则表达式特征允许日文数字
struct jnum_traits : std::regex_traits<wchar_t>
{
static std::map<wchar_t, int> data;
int value(wchar_t ch, int radix) const
{
wchar_t up = std::toupper(ch, getloc());
return data.count(up) ? data[up] : regex_traits::value(ch, radix);
}
};
std::map<wchar_t, int> jnum_traits::data = {{L'〇',0}, {L'一',1}, {L'二',2},
{L'三',3}, {L'四',4}, {L'五',5},
{L'六',6}, {L'七',7}, {L'八',8},
{L'九',9}, {L'A',10}, {L'B',11},
{L'C',12}, {L'D',13}, {L'E',14},
{L'F',15}};
int main()
{
std::locale::global(std::locale("ja_JP.utf8"));
std::wcout.sync_with_stdio(false);
std::wcout.imbue(std::locale());
std::wstring in = L"風";
if (std::regex_match(in, std::wregex(L"\\u98a8")))
std::wcout << "\\u98a8 匹配 " << in << '\n';
if (std::regex_match(in, std::basic_regex<wchar_t, jnum_traits>(L"\\u九八a八")))
std::wcout << L"\\u九八a八 以自定义特征匹配 " << in << '\n';
}
输出:
\u98a8 匹配 風
\u九八a八 以自定义特征匹配 風