std::regex_traits<CharT>::lookup_classname
template< class ForwardIt > char_class_type lookup_classname( ForwardIt first, |
||
如果字符序列 [
first,
last)
表示当前植入的区域设置中有效字符类的名称(即,正则表达式中 [:
和 :]
之间的字符串),则返回表示此字符类的实现定义值。否则,返回零。
如果参数 icase 为 true,则字符类忽略字符大小写,例如,带有 std::regex_constants::icase 的正则表达式 [:lower:]
生成对 std::regex_traits<>::lookup_classname() 的调用,其中 [
first,
last)
指示字符串 "lower" 且 icase == true。此调用返回的位掩码与带有 icase == false 的正则表达式 [:alpha:]
生成的调用相同。
以下窄字符和宽字符类名称始终被 std::regex_traits<char> 和 std::regex_traits<wchar_t> 分别识别,并且返回的分类(使用 icase == false)对应于通过植入的区域设置的 std::ctype facet 获得的匹配分类,如下所示
字符类名称 | std::ctype 分类 | |
---|---|---|
窄字符 | 宽字符 | |
"alnum" | L"alnum" | std::ctype_base::alnum |
"alpha" | L"alpha" | std::ctype_base::alpha |
"blank" | L"blank" | std::ctype_base::blank |
"cntrl" | L"cntrl" | std::ctype_base::cntrl |
"digit" | L"digit" | std::ctype_base::digit |
"graph" | L"graph" | std::ctype_base::graph |
"lower" | L"lower" | std::ctype_base::lower |
"print" | L"print" | std::ctype_base::print |
"punct" | L"punct" | std::ctype_base::punct |
"space" | L"space" | std::ctype_base::space |
"upper" | L"upper" | std::ctype_base::upper |
"xdigit" | L"xdigit" | std::ctype_base::xdigit |
"d" | L"d" | std::ctype_base::digit |
"s" | L"s" | std::ctype_base::space |
"w" | L"w" | std::ctype_base::alnum 可以选择性地添加 '_' |
为字符串 "w" 返回的分类可能与 "alnum" 完全相同,在这种情况下,isctype() 会显式添加 '_'。
系统提供的区域设置可能会提供其他分类,例如 "jdigit" 或 "jkanji"(在这种情况下,它们也可以通过 std::wctype 访问)。
目录 |
[编辑] 参数
first, last | - | 一对迭代器,用于确定表示字符类名称的字符序列 |
icase | - | 如果为 true,则忽略字符分类中的大小写区别 |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 |
[编辑] 返回值
表示给定字符类确定的字符分类的位掩码,如果类未知,则为 char_class_type()。
[编辑] 示例
演示 lookup_classname()
/ isctype() 的自定义正则表达式特性实现
#include <cwctype> #include <iostream> #include <locale> #include <regex> // This custom regex traits uses wctype/iswctype to implement lookup_classname/isctype. struct wctype_traits : std::regex_traits<wchar_t> { using char_class_type = std::wctype_t; template<class It> char_class_type lookup_classname(It first, It last, bool = false) const { return std::wctype(std::string(first, last).c_str()); } bool isctype(wchar_t c, char_class_type f) const { return std::iswctype(c, f); } }; int main() { std::locale::global(std::locale("ja_JP.utf8")); std::wcout.sync_with_stdio(false); std::wcout.imbue(std::locale()); std::wsmatch m; std::wstring in = L"風の谷のナウシカ"; // matches all characters (they are classified as alnum) std::regex_search(in, m, std::wregex(L"([[:alnum:]]+)")); std::wcout << "alnums: " << m[1] << '\n'; // prints "風の谷のナウシカ" // matches only the katakana std::regex_search(in, m, std::basic_regex<wchar_t, wctype_traits>(L"([[:jkata:]]+)")); std::wcout << "katakana: " << m[1] << '\n'; // prints "ナウシカ" }
输出
alnums: 風の谷のナウシカ katakana: ナウシカ
[编辑] 参见
指示字符类中的成员资格 (公共成员函数) | |
在当前的 C 区域设置中查找字符分类类别 (函数) |