REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::compat Namespace Reference

Drop-in replacements for <regex>: basic_regex, regex_search / regex_match / regex_replace and the iterator types – backed by REAL's linear-time engine where it can serve the pattern, and by std::regex otherwise, never by a silent divergence. More...

Namespaces

namespace  detail
 Backend routing and format expansion for the compat layer. Not a stable API.
 
namespace  re2
 Drop-in replacement for RE2's API surface, on REAL's engine.
 
namespace  regex_constants
 Compatibility constants mirroring std::regex_constants (own values, mapped internally).
 

Classes

class  basic_regex
 A std::basic_regex-compatible pattern, backed by real where proven, else std. More...
 
class  match_results
 The result of a match: group sub-matches plus the prefix and suffix. More...
 
class  regex_error
 std::regex_error-compatible exception. More...
 
class  regex_iterator
 Iterates the non-overlapping matches of a pattern in a sequence (std::regex_iterator). More...
 
class  regex_token_iterator
 Enumerates selected sub-matches (or the text between matches) — std::regex_token_iterator. More...
 
class  sub_match
 A matched sub-expression: a [first, second) range into the searched sequence. More...
 

Typedefs

using regex = basic_regex< char >
 The char-path compat regex (real-eligible).
 
using wregex = basic_regex< wchar_t >
 The wide compat regex (always the std backend).
 
using sregex_iterator = regex_iterator< std::string::const_iterator >
 Over a std::string.
 
using cregex_iterator = regex_iterator< const char * >
 Over a C string.
 
using wsregex_iterator = regex_iterator< std::wstring::const_iterator >
 Over a std::wstring (std).
 
using wcregex_iterator = regex_iterator< const wchar_t * >
 Over a wide C string (std).
 
using sregex_token_iterator = regex_token_iterator< std::string::const_iterator >
 Over a std::string.
 
using cregex_token_iterator = regex_token_iterator< const char * >
 Over a C string.
 
using wsregex_token_iterator = regex_token_iterator< std::wstring::const_iterator >
 Over a std::wstring (std).
 
using wcregex_token_iterator = regex_token_iterator< const wchar_t * >
 Over a wide C string (std).
 
using ssub_match = sub_match< std::string::const_iterator >
 Sub-match over a std::string.
 
using csub_match = sub_match< const char * >
 Sub-match over a C string.
 
using wssub_match = sub_match< std::wstring::const_iterator >
 Sub-match over a std::wstring.
 
using wcsub_match = sub_match< const wchar_t * >
 Sub-match over a wide C string.
 
using smatch = match_results< std::string::const_iterator >
 Match over a std::string.
 
using cmatch = match_results< const char * >
 Match over a C string.
 
using wsmatch = match_results< std::wstring::const_iterator >
 Match over a std::wstring (always std).
 
using wcmatch = match_results< const wchar_t * >
 Match over a wide C string (always std).
 

Enumerations

enum class  policy : std::uint8_t { strict , fallback }
 The drop-in policy for a pattern the linear engine cannot represent (backreferences, an unbounded lookaround, a POSIX class, …). strict (the default) rejects it, so every accepted pattern executes each regex_search/regex_match in time linear in the input — the ReDoS-safety guarantee (replace/iterate compose O(n) such operations: quadratic worst-case on any linear engine, never exponential); fallback delegates it to std::regex, which may accept it but forfeits the guarantee for that pattern. More...
 

Functions

template<typename BidirIt >
bool operator== (const sub_match< BidirIt > &lhs, const typename sub_match< BidirIt >::string_type &rhs)
 Equality against an owned string (the common std::sub_match comparison).
 
template<typename BidirIt >
bool operator== (const typename sub_match< BidirIt >::string_type &lhs, const sub_match< BidirIt > &rhs)
 Equality with the string on the left, for std::sub_match parity.
 
template<typename BidirIt >
bool operator== (const sub_match< BidirIt > &lhs, const sub_match< BidirIt > &rhs)
 Equality between two sub-matches, by matched text.
 
template<typename CharT , typename Traits , typename BidirIt >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const sub_match< BidirIt > &m)
 Stream the matched text (std::sub_match parity). Found by ADL from std::cout << m[1]. Writes m.str() — empty when the group did not participate.
 
template<typename BidirIt , typename CharT , typename Traits >
bool regex_search (BidirIt first, BidirIt last, match_results< BidirIt > &m, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Leftmost search of [first, last) (Python re.search / std::regex_search).
 
template<typename CharT , typename Traits >
bool regex_search (const std::basic_string< CharT > &s, match_results< typename std::basic_string< CharT >::const_iterator > &m, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Leftmost search over a std::basic_string; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_search (const CharT *s, match_results< const CharT * > &m, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Leftmost search over a C string; forwards to the primary overload.
 
template<typename BidirIt , typename CharT , typename Traits >
bool regex_search (BidirIt first, BidirIt last, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Leftmost search over [first, last), without capturing; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_search (const std::basic_string< CharT > &s, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Leftmost search over a std::basic_string, without capturing; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_search (const CharT *s, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Leftmost search over a C string, without capturing; forwards to the primary overload.
 
template<typename BidirIt , typename CharT , typename Traits >
bool regex_match (BidirIt first, BidirIt last, match_results< BidirIt > &m, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Match of the entire [first, last) (Python re.fullmatch / std::regex_match).
 
template<typename CharT , typename Traits >
bool regex_match (const std::basic_string< CharT > &s, match_results< typename std::basic_string< CharT >::const_iterator > &m, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Whole-sequence match over a std::basic_string; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_match (const CharT *s, match_results< const CharT * > &m, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Whole-sequence match over a C string; forwards to the primary overload.
 
template<typename BidirIt , typename CharT , typename Traits >
bool regex_match (BidirIt first, BidirIt last, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Whole-sequence match over [first, last), without capturing; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_match (const std::basic_string< CharT > &s, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Whole-sequence match over a std::basic_string, without capturing; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_match (const CharT *s, const basic_regex< CharT, Traits > &re, regex_constants::match_flag_type flags=regex_constants::match_default)
 Whole-sequence match over a C string, without capturing; forwards to the primary overload.
 
template<typename CharT , typename Traits >
bool regex_search (const std::basic_string< CharT > &&, match_results< typename std::basic_string< CharT >::const_iterator > &, const basic_regex< CharT, Traits > &)=delete
 
template<typename CharT , typename Traits >
bool regex_search (const std::basic_string< CharT > &&, match_results< typename std::basic_string< CharT >::const_iterator > &, const basic_regex< CharT, Traits > &, regex_constants::match_flag_type)=delete
 
template<typename CharT , typename Traits >
bool regex_match (const std::basic_string< CharT > &&, match_results< typename std::basic_string< CharT >::const_iterator > &, const basic_regex< CharT, Traits > &)=delete
 
template<typename CharT , typename Traits >
bool regex_match (const std::basic_string< CharT > &&, match_results< typename std::basic_string< CharT >::const_iterator > &, const basic_regex< CharT, Traits > &, regex_constants::match_flag_type)=delete
 
template<typename CharT , typename Traits >
std::basic_string< CharT > regex_replace (const std::basic_string< CharT > &s, const basic_regex< CharT, Traits > &re, const std::basic_string< CharT > &fmt, regex_constants::match_flag_type flags=regex_constants::format_default)
 Replaces matches of re in s with the ECMAScript-formatted fmt.
 
template<typename CharT , typename Traits >
std::basic_string< CharT > regex_replace (const std::basic_string< CharT > &s, const basic_regex< CharT, Traits > &re, const CharT *fmt, regex_constants::match_flag_type flags=regex_constants::format_default)
 regex_replace overload for a C-string format.
 
template<typename OutputIt , typename BidirIt , typename CharT , typename Traits >
OutputIt regex_replace (OutputIt out, BidirIt first, BidirIt last, const basic_regex< CharT, Traits > &re, const std::basic_string< CharT > &fmt, regex_constants::match_flag_type flags=regex_constants::format_default)
 regex_replace writing to an output iterator (std parity).
 
template<typename OutputIt , typename BidirIt , typename CharT , typename Traits >
OutputIt regex_replace (OutputIt out, BidirIt first, BidirIt last, const basic_regex< CharT, Traits > &re, const CharT *fmt, regex_constants::match_flag_type flags=regex_constants::format_default)
 regex_replace to an output iterator with a C-string format (std parity). Mirrors the string+const CharT* overload — a bare literal "+" decays to const CharT*.
 

Detailed Description

Drop-in replacements for <regex>: basic_regex, regex_search / regex_match / regex_replace and the iterator types – backed by REAL's linear-time engine where it can serve the pattern, and by std::regex otherwise, never by a silent divergence.

Enumeration Type Documentation

◆ policy

enum class real::compat::policy : std::uint8_t
strong

The drop-in policy for a pattern the linear engine cannot represent (backreferences, an unbounded lookaround, a POSIX class, …). strict (the default) rejects it, so every accepted pattern executes each regex_search/regex_match in time linear in the input — the ReDoS-safety guarantee (replace/iterate compose O(n) such operations: quadratic worst-case on any linear engine, never exponential); fallback delegates it to std::regex, which may accept it but forfeits the guarantee for that pattern.

Enumerator
strict 

Reject an ineligible pattern (throws regex_error with error_complexity). The default.

fallback 

Delegate an ineligible pattern to std::regex (backtracking — not ReDoS-safe).

Function Documentation

◆ operator<<()

template<typename CharT , typename Traits , typename BidirIt >
std::basic_ostream< CharT, Traits > & real::compat::operator<< ( std::basic_ostream< CharT, Traits > &  os,
const sub_match< BidirIt > &  m 
)

Stream the matched text (std::sub_match parity). Found by ADL from std::cout << m[1]. Writes m.str() — empty when the group did not participate.

Parameters
[in,out]osThe stream.
[in]mThe sub-match whose text is written.
Returns
os, after writing.

◆ operator==() [1/3]

template<typename BidirIt >
bool real::compat::operator== ( const sub_match< BidirIt > &  lhs,
const sub_match< BidirIt > &  rhs 
)

Equality between two sub-matches, by matched text.

Parameters
[in]lhsThe left sub-match.
[in]rhsThe right sub-match.
Returns
true if they hold the same characters.

◆ operator==() [2/3]

template<typename BidirIt >
bool real::compat::operator== ( const sub_match< BidirIt > &  lhs,
const typename sub_match< BidirIt >::string_type &  rhs 
)

Equality against an owned string (the common std::sub_match comparison).

Parameters
[in]lhsThe sub-match.
[in]rhsThe string to compare its text against.
Returns
true if they hold the same characters.

◆ operator==() [3/3]

template<typename BidirIt >
bool real::compat::operator== ( const typename sub_match< BidirIt >::string_type &  lhs,
const sub_match< BidirIt > &  rhs 
)

Equality with the string on the left, for std::sub_match parity.

Parameters
[in]lhsThe string.
[in]rhsThe sub-match whose text is compared.
Returns
true if they hold the same characters.

◆ regex_match() [1/6]

template<typename BidirIt , typename CharT , typename Traits >
bool real::compat::regex_match ( BidirIt  first,
BidirIt  last,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Whole-sequence match over [first, last), without capturing; forwards to the primary overload.

Parameters
[in]firstStart of the sequence.
[in]lastOne past its end.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if the whole sequence matched.

◆ regex_match() [2/6]

template<typename BidirIt , typename CharT , typename Traits >
bool real::compat::regex_match ( BidirIt  first,
BidirIt  last,
match_results< BidirIt > &  m,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Match of the entire [first, last) (Python re.fullmatch / std::regex_match).

   The other overloads forward here; those taking no \ref match_results skip capture filling.
Parameters
[in]firstStart of the sequence that must match in full.
[in]lastOne past its end.
[out]mResult filled on success; ready-but-unmatched on failure.
[in]reThe pattern.
[in]flagsMatch flags; a constraining one routes to std.
Returns
true if the whole sequence matched.

◆ regex_match() [3/6]

template<typename CharT , typename Traits >
bool real::compat::regex_match ( const CharT *  s,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Whole-sequence match over a C string, without capturing; forwards to the primary overload.

Parameters
[in]sThe subject.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if the whole sequence matched.

◆ regex_match() [4/6]

template<typename CharT , typename Traits >
bool real::compat::regex_match ( const CharT *  s,
match_results< const CharT * > &  m,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Whole-sequence match over a C string; forwards to the primary overload.

Parameters
[in]sThe subject.
[out]mResult filled on success.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if the whole sequence matched.

◆ regex_match() [5/6]

template<typename CharT , typename Traits >
bool real::compat::regex_match ( const std::basic_string< CharT > &  s,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Whole-sequence match over a std::basic_string, without capturing; forwards to the primary overload.

Parameters
[in]sThe subject.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if the whole sequence matched.

◆ regex_match() [6/6]

template<typename CharT , typename Traits >
bool real::compat::regex_match ( const std::basic_string< CharT > &  s,
match_results< typename std::basic_string< CharT >::const_iterator > &  m,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Whole-sequence match over a std::basic_string; forwards to the primary overload.

Parameters
[in]sThe subject.
[out]mResult filled on success.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if the whole sequence matched.

◆ regex_replace() [1/4]

template<typename CharT , typename Traits >
std::basic_string< CharT > real::compat::regex_replace ( const std::basic_string< CharT > &  s,
const basic_regex< CharT, Traits > &  re,
const CharT *  fmt,
regex_constants::match_flag_type  flags = regex_constants::format_default 
)

regex_replace overload for a C-string format.

Parameters
[in]sThe subject.
[in]reThe pattern whose matches are replaced.
[in]fmtECMAScript replacement format, as a C string.
[in]flagsMatch/format flags.
Returns
The subject with every match replaced.

◆ regex_replace() [2/4]

template<typename CharT , typename Traits >
std::basic_string< CharT > real::compat::regex_replace ( const std::basic_string< CharT > &  s,
const basic_regex< CharT, Traits > &  re,
const std::basic_string< CharT > &  fmt,
regex_constants::match_flag_type  flags = regex_constants::format_default 
)

Replaces matches of re in s with the ECMAScript-formatted fmt.

Real-backed, non-nullable patterns run the substitution on real (linear, ReDoS-safe); the std backend and nullable real-backed patterns route to std::regex_replace (the empty-match traversal differs between Python real and ECMAScript, see basic_regex::nullable).

The other overloads forward here.

Parameters
[in]sThe subject.
[in]reThe pattern whose matches are replaced.
[in]fmtECMAScript replacement format; $N refers to a group, $& to the whole match.
[in]flagsMatch/format flags; one the real expander does not honor routes to std.
Returns
The subject with every match replaced.

◆ regex_replace() [3/4]

template<typename OutputIt , typename BidirIt , typename CharT , typename Traits >
OutputIt real::compat::regex_replace ( OutputIt  out,
BidirIt  first,
BidirIt  last,
const basic_regex< CharT, Traits > &  re,
const CharT *  fmt,
regex_constants::match_flag_type  flags = regex_constants::format_default 
)

regex_replace to an output iterator with a C-string format (std parity). Mirrors the string+const CharT* overload — a bare literal "+" decays to const CharT*.

Parameters
[out]outDestination the result is written through.
[in]firstStart of the subject sequence.
[in]lastOne past its end.
[in]reThe pattern whose matches are replaced.
[in]fmtECMAScript replacement format, as a C string.
[in]flagsMatch/format flags.
Returns
out advanced past what was written.

◆ regex_replace() [4/4]

template<typename OutputIt , typename BidirIt , typename CharT , typename Traits >
OutputIt real::compat::regex_replace ( OutputIt  out,
BidirIt  first,
BidirIt  last,
const basic_regex< CharT, Traits > &  re,
const std::basic_string< CharT > &  fmt,
regex_constants::match_flag_type  flags = regex_constants::format_default 
)

regex_replace writing to an output iterator (std parity).

Parameters
[out]outDestination the result is written through.
[in]firstStart of the subject sequence.
[in]lastOne past its end.
[in]reThe pattern whose matches are replaced.
[in]fmtECMAScript replacement format.
[in]flagsMatch/format flags.
Returns
out advanced past what was written.

◆ regex_search() [1/6]

template<typename BidirIt , typename CharT , typename Traits >
bool real::compat::regex_search ( BidirIt  first,
BidirIt  last,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Leftmost search over [first, last), without capturing; forwards to the primary overload.

Parameters
[in]firstStart of the sequence.
[in]lastOne past its end.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if a match was found.

◆ regex_search() [2/6]

template<typename BidirIt , typename CharT , typename Traits >
bool real::compat::regex_search ( BidirIt  first,
BidirIt  last,
match_results< BidirIt > &  m,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Leftmost search of [first, last) (Python re.search / std::regex_search).

   The other overloads forward here; those taking no \ref match_results skip capture filling.
Parameters
[in]firstStart of the sequence to search.
[in]lastOne past its end.
[out]mResult filled on success; ready-but-unmatched on failure, as std leaves it.
[in]reThe pattern.
[in]flagsMatch flags; a constraining one routes to std.
Returns
true if a match was found.

◆ regex_search() [3/6]

template<typename CharT , typename Traits >
bool real::compat::regex_search ( const CharT *  s,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Leftmost search over a C string, without capturing; forwards to the primary overload.

Parameters
[in]sThe subject.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if a match was found.

◆ regex_search() [4/6]

template<typename CharT , typename Traits >
bool real::compat::regex_search ( const CharT *  s,
match_results< const CharT * > &  m,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Leftmost search over a C string; forwards to the primary overload.

Parameters
[in]sThe subject.
[out]mResult filled on success.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if a match was found.

◆ regex_search() [5/6]

template<typename CharT , typename Traits >
bool real::compat::regex_search ( const std::basic_string< CharT > &  s,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Leftmost search over a std::basic_string, without capturing; forwards to the primary overload.

Parameters
[in]sThe subject.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if a match was found.

◆ regex_search() [6/6]

template<typename CharT , typename Traits >
bool real::compat::regex_search ( const std::basic_string< CharT > &  s,
match_results< typename std::basic_string< CharT >::const_iterator > &  m,
const basic_regex< CharT, Traits > &  re,
regex_constants::match_flag_type  flags = regex_constants::match_default 
)

Leftmost search over a std::basic_string; forwards to the primary overload.

Parameters
[in]sThe subject.
[out]mResult filled on success.
[in]reThe pattern.
[in]flagsMatch flags.
Returns
true if a match was found.