std::regex_traits::value

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 <regex>
#include <map>
 
// 此定制正则表达式特性允许日文数字
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 風

版本历史

  • (当前 | 先前 2017年10月16日 (一) 09:24Fruderica讨论 | 贡献 . . (2,268字节) (+6). . (correct) (撤销)
  • 当前 | 先前 2017年10月13日 (五) 09:50Fruderica讨论 | 贡献. . (2,262字节) (+996). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:07P12bot讨论 | 贡献 . . (1,266字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:16P12bot讨论 | 贡献 . . (1,266字节) (-61). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 11:26P12bot讨论 | 贡献 . . (1,327字节) (+289). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 12:11P12讨论 | 贡献 . . (1,038字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,038字节) (-39). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 15:33P12讨论 | 贡献 . . (1,077字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,077字节) (+1,077). . (Translated from the English version using Google Translate)