首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::regex_traits::value

int value( CharT ch, int radix ) const;

?

(since C++11)

确定由数字表示的值。ch在数字基中radix,考虑到当前注入的环境。此函数由std::regex处理时量词{1}或{2,5}反向引用\1和十六进制和Unicode字符转义。

参数

ch

-

the character that may represent a digit

radix

-

either 8, 10, or 16

返回值

如果ch实际上,表示当前注入的区域设置中对数字基有效的数字。radix,或-1关于错误。

二次

代码语言:javascript
复制
#include <iostream>
#include <locale>
#include <regex>
#include <map>
 
// 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';
}

二次

产出:

二次

代码语言:javascript
复制
\u98a8 matched 風
\u九八a八 with custom traits matched 風

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com