std::regex_traits<CharT>::isctype
來自 cppreference.com
< cpp | regex | regex_traits
bool isctype( CharT c, char_class_type f ) const; |
||
判斷字元 c 是否屬於由 f 標識的字元類別,其中 f 是 lookup_classname() 返回的值,或者是多個此類值的位或結果。
標準庫中 std::regex_traits 特化版本提供的此函式執行以下操作:
2) 然後嘗試透過呼叫 std::use_facet<std::ctype<CharT>>(getloc()).is(m, c) 來分類 imbued 區域設定中的字元。
- 如果返回 true,則
isctype()
也將返回 true。 - 否則,如果 c 等於 '_',並且 f 包含對字元類別
[:w:]
呼叫 lookup_classname() 的結果,則返回 true,否則返回 false。
目錄 |
[編輯] 引數
c | - | 要分類的字元 |
f | - | 從一次或多次呼叫 lookup_classname() 獲得的位掩碼 |
[編輯] 返回值
如果 c 被 f 分類,則返回 true,否則返回 false。
[編輯] 示例
執行此程式碼
#include <iostream> #include <regex> #include <string> int main() { std::regex_traits<char> t; std::string str_alnum = "alnum"; auto a = t.lookup_classname(str_alnum.begin(), str_alnum.end()); std::string str_w = "w"; // [:w:] is [:alnum:] plus '_' auto w = t.lookup_classname(str_w.begin(), str_w.end()); std::cout << std::boolalpha << t.isctype('A', w) << ' ' << t.isctype('A', a) << '\n' << t.isctype('_', w) << ' ' << t.isctype('_', a) << '\n' << t.isctype(' ', w) << ' ' << t.isctype(' ', a) << '\n'; }
輸出
true true true false false false
演示 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2018 | C++11 | m 的值未指定 | 匹配 lookup_classname() 的最小支援 |
[編輯] 參閱
按名稱獲取字元類別 (public 成員函式) | |
[virtual] |
分類一個字元或一個字元序列 ( std::ctype<CharT> 的虛保護成員函式) |
根據指定的 LC_CTYPE 類別對寬字元進行分類(函式) |