std::basic_regex(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::basic_regex

okay so bestie, buckle up β€” this one's from the C++ Standard Library manual (section 3, yes it's literally a "Libary" in the original header and we respect the typo 🫢). It's giving regex but make it a class template, and honestly? Iconic.

✨ NAME ✨

std::basic_regex β€” which is, and I cannot stress this enough, std::basic_regex πŸ’€

🌸 SYNOPSIS 🌸

Defined in header <regex>:

template<

    class CharT,                             (since C++11)
    class Traits = std::regex_traits<CharT>

> class basic_regex;

So like β€” the class template basic_regex provides a general framework for holding regular expressions. That's it, that's the whole job, and it does it slayfully.

There are also several typedefs for the common character types, pre-baked for your convenience πŸŽ€ β€” defined in header <regex>:

  • std::regex β€” defined as std::basic_regex<char>
  • std::wregex β€” defined as std::basic_regex<wchar_t>

πŸ¦‹ Member types πŸ¦‹

the little type-alias family photo:

  • value_type β€” defined as CharT
  • traits_type β€” defined as Traits
  • string_type β€” defined as Traits::string_type
  • locale_type β€” defined as Traits::locale_type
  • flag_type β€” defined as std::regex_constants::syntax_option_type

🌈 Member functions 🌈

  • constructor β€” constructs the regex object (public member function)
  • destructor β€” destructs the regex object (public member function)
  • operator= β€” assigns the contents (public member function)
  • assign β€” assigns the contents (public member function)

⭐ Observers ⭐

  • mark_count β€” returns the number of marked sub-expressions within the regular expression (public member function)
  • flags β€” returns the syntax flags (public member function)

πŸ’– Locale πŸ’–

  • getloc β€” get locale information (public member function)
  • imbue β€” set locale information (public member function)

🧚 Modifiers 🧚

  • swap β€” swaps the contents (public member function)

πŸ‘‘ CONSTANTS πŸ‘‘

okay this is the part where you actually pay attention, bestie, this is the main character section πŸ”₯

Grammer options (grammar options, but make it cute)

  • ECMAScript β€” Use the Modified ECMAScript regular expression grammar.
  • basic β€” Use the basic POSIX regular expression grammar (grammar documentation).
  • extended β€” Use the extended POSIX regular expression grammar (grammar documentation).
  • awk β€” Use the regular expression grammar used by the awk utility in POSIX (grammar documentation).
  • grep β€” Use the regular expression grammar used by the grep utility in POSIX. This is effectively the same as the basic option with the addition of newline '\n' as an alternation separator.
  • egrep β€” Use the regular expression grammar used by the grep utility, with the -E option, in POSIX. This is effectively the same as the extended option with the addition of newline '\n' as an alternation separator in addition to '|'.

Grammar variations

  • icase β€” Character matching should be performed without regard to case.
  • nosubs β€” When performing matches, all marked sub-expressions (expr) are treated as non-marking sub-expressions (?:expr). No matches are stored in the supplied std::regex_match structure and mark_count() is zero.
  • optimize β€” Instructs the regular expression engine to make matching faster, with the potential cost of making construction slower. For example, this might mean converting a non-deterministic FSA to a deterministic FSA.
  • collate β€” Character ranges of the form "[a-b]" will be locale sensitive.
  • multiline (C++17) β€” Specifies that ^ shall match the beginning of a line and $ shall match the end of a line, if the ECMAScript engine is selected.
⚠️ rule check, no cap: at most one grammar option can be chosen out of ECMAScript, basic, extended, awk, grep, egrep. If no grammar is chosen, ECMAScript is assumed to be selected. The other options serve as variations, such that std::regex("meow", std::regex::icase) is equivalent to std::regex("meow", std::regex::ECMAScript|std::regex::icase). (yes the manpage really said meow. we love them for that 🐱)

And a lil detail that matters: the member constants in basic_regex are duplicates of the syntax_option_type constants defined in the namespace std::regex_constants. fyi πŸ’…

🫢 NON-MEMBER FUNCTIONS 🫢

  • std::swap(std::basic_regex) (C++11) β€” specializes the std::swap algorithm (function template)

πŸ“ Deduction guides (since C++17) πŸ“

yup, they exist, go look at 'em πŸ’œ


✨ and that's the whole page ✨ β€” brought to you by http://cppreference.com, dated 2024.06.10, and this absolutely iconic std::basic_regex(3) documentation. mwah πŸ’‹

πŸ“¦ where this page comes from