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
。此调用返回的位掩码与正则表达式
[:alpha:]
在
icase
==
false
时生成的调用返回的位掩码相同。
以下窄字符和宽字符类名称始终分别由 std:: regex_traits < char > 和 std:: regex_traits < wchar_t > 识别,且返回的分类(当 icase == false 时)对应于通过所植入区域设置的 std::ctype 刻面获得的匹配分类,具体如下:
| 字符类名称 | 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> // 此自定义正则表达式特性使用 wctype/iswctype 实现 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"風の谷のナウシカ"; // 匹配所有字符(它们被分类为字母数字) std::regex_search(in, m, std::wregex(L"([[:alnum:]]+)")); std::wcout << "alnums: " << m[1] << '\n'; // 输出 "風の谷のナウシカ" // 仅匹配片假名 std::regex_search(in, m, std::basic_regex<wchar_t, wctype_traits>(L"([[:jkata:]]+)")); std::wcout << "katakana: " << m[1] << '\n'; // 输出 "ナウシカ" }
输出:
alnums: 風の谷のナウシカ katakana: ナウシカ
参见
|
指示字符类中的成员关系
(公开成员函数) |
|
|
在当前C语言区域中查找字符分类类别
(函数) |