名前空間
変種
操作

std::regex_traits<CharT>::value

From cppreference.com
< cpp‎ | regex‎ | regex traits
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
イテレータ
例外
Traits
定数
(C++11)
正規表現文法
 
 
int value( CharT ch, int radix ) const;
(C++11以降)

現在のロケールで、数値基数 radix における数字 ch が表す値を決定します。この関数は、std::regexQuantifiers(例:{1} や {2,5})、Backreferences(例:\1)、16進数およびUnicode文字エスケープを処理する際に呼び出されます。

[編集] パラメータ

文字 - 数字を表す可能性のある文字
radix - 8, 10, または 16

[編集] 戻り値

ch が現在のロケールで、数値基数 radix に対して有効な数字を表す場合はその数値、エラーの場合は -1

[編集]

#include <iostream>
#include <locale>
#include <map>
#include <regex>
 
// This custom regex traits allows japanese numerals
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 matched " << in << '\n';
 
    if (std::regex_match(in, std::basic_regex<wchar_t, jnum_traits>(L"\\u九八a八")))
        std::wcout << L"\\u九八a八 with custom traits matched " << in << '\n';
}

出力

\u98a8 matched 風
\u九八a八 with custom traits matched 風
English 日本語 中文(简体) 中文(繁體)