名稱空間
變體
操作

std::regex_traits<CharT>::lookup_classname

來自 cppreference.com
< cpp‎ | regex‎ | regex_traits
 
 
 
正則表示式庫
(C++11)
演算法
迭代器
異常
特性
常量
(C++11)
正則表示式語法
 
 
template< class ForwardIt >

char_class_type lookup_classname( ForwardIt first,
                                  ForwardIt last,

                                  bool icase = false ) const;

如果字元序列 [firstlast) 表示當前被植入的區域設定中有效字元類的名稱(即正則表示式中 [::] 之間的字串),則返回表示此字元類的實現定義值。否則,返回零。

如果引數 icasetrue,則字元類會忽略字元大小寫,例如,帶有 std::regex_constants::icase 的正則表示式 [:lower:] 會生成對 std::regex_traits<>::lookup_classname() 的呼叫,其中 [firstlast) 指示字串 "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 語言環境中查詢字元分類類別
(函式) [編輯]