std::regex_...option_type(3)
β¨ NAME β¨
Okay so bestie, welcome to the manual page for std::regex_constants::syntax_option_type β yes, the whole gorgeous mouthful. This is from the C++ Standard Library (manual label: std::regex_...option_type(3)), and itβs literally the BitmaskType that holds the options governing how regular expressions behave. π
π¦ Synopsis π¦
Defined in header <regex>, and here is the entire synopsis vibe, no cap:
using syntax_option_type = /* implementation-defined (1) (since C++11) */;
constexpr syntax_option_type icase = /* unspecified */;
constexpr syntax_option_type nosubs = /* unspecified */;
constexpr syntax_option_type optimize = /* unspecified */;
constexpr syntax_option_type collate = /* unspecified */;
constexpr syntax_option_type ECMAScript = /* unspecified */;
(since C++11)
constexpr syntax_option_type basic = /* (until C++17) unspecified */;
constexpr syntax_option_type extended = /* unspecified */;
constexpr syntax_option_type awk = /* unspecified */;
constexpr syntax_option_type grep = /* unspecified */;
constexpr syntax_option_type egrep = /* unspecified */;
inline constexpr syntax_option_type icase = /* (2) unspecified */;
inline constexpr syntax_option_type nosubs = /* unspecified */;
inline constexpr syntax_option_type optimize = /* unspecified */;
inline constexpr syntax_option_type collate = /* unspecified */;
inline constexpr syntax_option_type ECMAScript = /* unspecified */;
(since C++17)
inline constexpr syntax_option_type basic = /* unspecified */;
inline constexpr syntax_option_type extended = /* unspecified */;
inline constexpr syntax_option_type awk = /* unspecified */;
inline constexpr syntax_option_type grep = /* unspecified */;
inline constexpr syntax_option_type egrep = /* unspecified */;
inline constexpr syntax_option_type multiline = /* (3) (since C++17) unspecified */;
- The
syntax_option_typeis aBitmaskTypethat contains options that govern how regular expressions behave.
2,3) The possible values (icase, optimize, etc.) for type (1) are duplicated inside std::basic_regex.
πΈ Constants πΈ
Grammar option β effect(s)
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 theawkutility in POSIX (grammar documentation).grepβ Use the regular expression grammar used by thegreputility in POSIX. This is effectively the same as thebasicoption with the addition of newline'\n'as an alternation separator.egrepβ Use the regular expression grammar used by thegreputility, with the-Eoption, in POSIX. This is effectively the same as theextendedoption with the addition of newline'\n'as an alternation separator in addition to'|'.
Grammar variation β effect(s)
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 suppliedstd::regex_matchstructure andmark_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.
Important vibe check: At most one grammar option can be chosen out ofECMAScript,basic,extended,awk,grep,egrep. If no grammar is chosen,ECMAScriptis assumed to be selected. The other options serve as variations, such thatstd::regex("meow", std::regex::icase)is equivalent tostd::regex("meow", std::regex::ECMAScript|std::regex::icase).
π Notes π
Because POSIX uses the "leftmost longest" matching rule (the longest matching subsequence is matched, and if there are several such subsequences, the first one is matched), it is not suitable, for example, for parsing markup languages: a POSIX regex such as "<tag[^>]*>.*</tag>" would match everything from the first "<tag" to the last "</tag>", including every "</tag>" and "<tag>" in-between. On the other hand, ECMAScript supports non-greedy matches, and the ECMAScript regex "<tag[^>]*>.*?</tag>" would match only until the first closing tag.
π Example π
This example is literally showing off the matching-algorithm glow-up difference between ECMAScript and POSIX regular expressions:
// Run this code
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string str = "zzxayyzz";
std::regex re1(".*(a|xayy)"); // ECMA
std::regex re2(".*(a|xayy)", std::regex::extended); // POSIX
std::cout << "Searching for .*(a|xayy) in zzxayyzz:\n";
std::smatch m;
std::regex_search(str, m, re1);
std::cout << " ECMA (depth first search) match: " << m[0] << '\n';
std::regex_search(str, m, re2);
std::cout << " POSIX (leftmost longest) match: " << m[0] << '\n';
}
β Output β
Searching for .*(a|xayy) in zzxayyzz:
ECMA (depth first search) match: zzxa
POSIX (leftmost longest) match: zzxayy
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 2053 - Applied to:
C++11 - Behavior as published: the constants were declared
static - Correct behavior: removed the
staticspecifier
π See also π
basic_regexβ regular expression object(C++11)(class template)
Thatβs the whole page, bestie! Source/version/date sign-off vibes: http://cppreference.com, 2024.06.10, and the manual label std::regex_...option_type(3) β now go slay your regexes. π«Άπ
π¦ 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