std::regex_...up_classname(3)
๐ 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"/ wideL"alnum"โstd::ctype_base::alnum - narrow
"alpha"/ wideL"alpha"โstd::ctype_base::alpha - narrow
"blank"/ wideL"blank"โstd::ctype_base::blank - narrow
"cntrl"/ wideL"cntrl"โstd::ctype_base::cntrl - narrow
"digit"/ wideL"digit"โstd::ctype_base::digit - narrow
"graph"/ wideL"graph"โstd::ctype_base::graph - narrow
"lower"/ wideL"lower"โstd::ctype_base::lower - narrow
"print"/ wideL"print"โstd::ctype_base::print - narrow
"punct"/ wideL"punct"โstd::ctype_base::punct - narrow
"space"/ wideL"space"โstd::ctype_base::space - narrow
"upper"/ wideL"upper"โstd::ctype_base::upper - narrow
"xdigit"/ wideL"xdigit"โstd::ctype_base::xdigit - narrow
"d"/ wideL"d"โstd::ctype_base::digit - narrow
"s"/ wideL"s"โstd::ctype_base::space - narrow
"w"/ wideL"w"โstd::ctype_base::alnumwith'_'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 classicaseโ if true, ignores the upper/lower case distinction in the character classification
๐ฆ Type requirements ๐ฆ
ForwardItmust 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 ยท MIT
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms