std::regex_...up_classname(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ‘‘ NAME ๐Ÿ‘‘

std::regex_traits::lookup_classname - std::regex_traits::lookup_classname

okay so this is the std::regex_...up_classname(3) page from the C++ Standard Libary (yes, that spelling is in the header, and honestly? iconic). bestie, we are about to decode how regex character classes get looked up, and it's so much less scary than it sounds ๐Ÿ’…โœจ


โœจ Synopsis โœจ

template< class ForwardIt >

char_class_type lookup_classname( ForwardIt first,
                                  ForwardIt last,

                                  bool icase = false ) const;

If the character sequence [first, last) represents the name of a valid character class in the currently imbued locale (that is, the string between [: and :] in regular expressions), returns the implementation-defined value representing this character class. Otherwise, returns zero.

If the parameter icase is true, the character class ignores character case, e.g. the regex [:lower:] with std::regex_constants::icase generates a call to std::regex_traits<>::lookup_classname() with [first, last) indicating the string "lower" and icase == true. This call returns the same bitmask as the call generated by the regex [:alpha:] with icase == false.

The following narrow and wide character class names are always recognized by std::regex_traits<char> and std::regex_traits<wchar_t> respectively, and the classifications returned (with icase == false) correspond to the matching classifications obtained by the std::ctype facet of the imbued locale, as follows:

  • narrow "alnum" / wide L"alnum" โ†’ std::ctype_base::alnum
  • narrow "alpha" / wide L"alpha" โ†’ std::ctype_base::alpha
  • narrow "blank" / wide L"blank" โ†’ std::ctype_base::blank
  • narrow "cntrl" / wide L"cntrl" โ†’ std::ctype_base::cntrl
  • narrow "digit" / wide L"digit" โ†’ std::ctype_base::digit
  • narrow "graph" / wide L"graph" โ†’ std::ctype_base::graph
  • narrow "lower" / wide L"lower" โ†’ std::ctype_base::lower
  • narrow "print" / wide L"print" โ†’ std::ctype_base::print
  • narrow "punct" / wide L"punct" โ†’ std::ctype_base::punct
  • narrow "space" / wide L"space" โ†’ std::ctype_base::space
  • narrow "upper" / wide L"upper" โ†’ std::ctype_base::upper
  • narrow "xdigit" / wide L"xdigit" โ†’ std::ctype_base::xdigit
  • narrow "d" / wide L"d" โ†’ std::ctype_base::digit
  • narrow "s" / wide L"s" โ†’ std::ctype_base::space
  • narrow "w" / wide L"w" โ†’ std::ctype_base::alnum with '_' optionally added

The classification returned for the string "w" may be exactly the same as "alnum", in which case isctype() adds '_' explicitly.

Additional classifications such as "jdigit" or "jkanji" may be provided by system-supplied locales (in which case they are also accessible through std::wctype).


๐ŸŒธ Parameters ๐ŸŒธ

  • first, last โ€” a pair of iterators which determines the sequence of characters that represents a name of a character class
  • icase โ€” if true, ignores the upper/lower case distinction in the character classification

๐Ÿฆ‹ Type requirements ๐Ÿฆ‹

  • ForwardIt must meet the requirements of LegacyForwardIterator.

๐Ÿ’– Return value ๐Ÿ’–

The bitmask representing the character classification determined by the given character class, or char_class_type() if the class is unknown.


๐ŸŽ€ Example ๐ŸŽ€

Demonstrates a custom regex traits implementation of lookup_classname() / isctype():

// Run this code

#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 "ใƒŠใ‚ฆใ‚ทใ‚ซ"
}

Output:

alnums: ้ขจใฎ่ฐทใฎใƒŠใ‚ฆใ‚ทใ‚ซ
katakana: ใƒŠใ‚ฆใ‚ทใ‚ซ

๐ŸŒˆ See also ๐ŸŒˆ

  • isctype โ€” indicates membership in a character class (public member function)
  • wctype โ€” looks up a character classification category in the current C locale (function)

okay bestie, that's the whole page, and you ate that regex class lookup lecture UP ๐Ÿ’…โœจ source: http://cppreference.com ยท version/date: 2024.06.10 ยท page: std::regex_...up_classname(3) ๐Ÿซถ๐ŸŽ€

๐Ÿ“ฆ where this page comes from