std::regex_traits::value
From cppreference.com
< cpplrm; | regexlrm; | regex traits
int value( CharT ch, int radix ) const; |
(since C++11) | |
Determines the value represented by the digit ch
in the numeric base radix
, given the currently imbued locale. This function is called by std::regex when processing Quantifiers such as {1
} or {2,5
}, Backreferences such as \1
, and hexadecimal and Unicode character escapes.
Parameters
ch | - | the character that may represent a digit |
radix | - | either 8, 10, or 16 |
Return value
The numeric value if ch
indeed represents a digit in the currently imbued locale that is valid for the numeric base radix
, or -1 on error.
Example
Run this code
#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'',10}, {L'',11}, {L'',12}, {L'',13}, {L'',14}, {L'',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"))) std::wcout << L"\\u with custom traits matched " << in << '\n'; }
Output:
\u98a8 matched \u with custom traits matched