Namespaces
Variants

std::regex_traits<CharT>:: value

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
int value ( CharT ch, int radix ) const ;
(C++11 이후)

현재 적용된 로케일을 고려하여 숫자 기수 radix 에서 숫자 ch 가 나타내는 값을 결정합니다. 이 함수는 std::regex Quantifiers (예: {1 } 또는 {2,5 }), Backreferences (예: \1 ), 그리고 16진수 및 유니코드 문자 이스케이프를 처리할 때 호출됩니다.

매개변수

ch - 숫자를 나타낼 수 있는 문자
radix - 8, 10, 또는 16 중 하나

반환값

ch 가 현재 적용된 로케일에서 유효한 숫자이며, 해당 숫자 기반 radix 에 대해 유효한 숫자를 나타내는 경우 해당 숫자 값, 오류 발생 시 - 1 을 반환합니다.

예제

#include <iostream>
#include <locale>
#include <map>
#include <regex>
// 이 사용자 정의 regex traits는 일본어 숫자를 허용합니다
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 風