regex_set#
Synopsis#
A set of patterns compiled together, answering which of them match a
subject: is_match (any of them – stops at the first hit), matches
(one bool per pattern) and which (the matching indices), always in
construction order. Individual members stay accessible as compiled
basic_regex objects.
Interface#
-
class regex_set#
Multi-pattern set: which patterns match the subject at least once.
Construction compiles every pattern (same flags as regex). If any pattern is invalid or unsupported, the constructor throws regex_error — there is no silent skip. Capture groups are not reported by the set; re-run the individual regex if groups are needed.
Bitset order is the construction order: index 0 is the first pattern, etc.
Public Functions
-
inline explicit regex_set(std::span<const std::string_view> patterns, flags compile_flags = flags::none)#
Compiles every pattern in
patterns(construction order = bitset order).- Parameters:
patterns – [in] Pattern texts; an empty set is allowed.
compile_flags – [in] Flags applied to every pattern (same as regex).
- Throws:
regex_error – if any pattern fails to compile.
-
inline regex_set(const std::string_view *patterns, std::size_t n, flags compile_flags = flags::none)#
Convenience: compile from a contiguous array of string views.
- Parameters:
patterns – [in] Pointer to the first pattern.
n – [in] Pattern count.
compile_flags – [in] Flags shared by every member.
-
inline regex_set(std::initializer_list<std::string_view> patterns, flags compile_flags = flags::none)#
Brace-init:
regex_set{"a", “b”, R”(\d+)”} .- Parameters:
patterns – [in] The patterns to compile.
compile_flags – [in] Flags shared by every member.
-
inline explicit regex_set(std::span<const std::string> patterns, flags compile_flags = flags::none)#
Compile from owning strings (e.g.
std::vector<std::string>).- Parameters:
patterns – [in] The patterns to compile; only borrowed for the duration of the call.
compile_flags – [in] Flags shared by every member.
-
inline std::size_t size() const noexcept#
Number of patterns in the set (bitset length).
- Returns:
The member count.
-
inline bool empty() const noexcept#
True if the set has no patterns.
- Returns:
Whether the set is empty.
-
inline flags compile_flags() const noexcept#
Compilation flags shared by every member.
- Returns:
The flag set every member was compiled with.
-
inline bool is_match(std::string_view text, std::size_t pos = 0, std::size_t endpos = npos) const#
True if any pattern matches the subject at least once.
Stops at the first matching pattern (any-match early exit). Region semantics match regex::search —
endpostruncates the view;posis the start offset (not a slice).- Parameters:
text – [in] Subject.
pos – [in] Byte offset the search starts at.
endpos – [in] Byte offset the region ends at; defaults to the end of
text.
- Returns:
Whether at least one pattern matches.
-
inline std::vector<bool> matches(std::string_view text, std::size_t pos = 0, std::size_t endpos = npos) const#
Which patterns match at least once (construction-order bitset).
Index
iis true iff patternimatched. Order is always construction order, whether the set walks members individually or through a fused scan.- Parameters:
text – [in] Subject.
pos – [in] Byte offset the search starts at.
endpos – [in] Byte offset the region ends at; defaults to the end of
text.
- Returns:
One bool per member, in construction order.
-
inline std::vector<std::size_t> which(std::string_view text, std::size_t pos = 0, std::size_t endpos = npos) const#
Indices of patterns that match (construction order, ascending).
- Parameters:
text – [in] Subject.
pos – [in] Byte offset the search starts at.
endpos – [in] Byte offset the region ends at; defaults to the end of
text.
- Returns:
The matching members’ construction indices, ascending.
-
inline explicit regex_set(std::span<const std::string_view> patterns, flags compile_flags = flags::none)#
Complexity#
Every scan is guaranteed linear in the text and never backtracks – each
member keeps the engine’s ReDoS-safe contract. A small set walks members
individually (is_match stops at the first hit). A large enough
DFA-eligible subset shares one fused pass; the bitset stays in construction
order either way. Numbers against RE2::Set live in
Performance.
Example#
Compiled and run by the example-check gate on every push:
// Compile the patterns together; ask which of them match a subject.
const real::regex_set routes {"^/api/", R"(\.json$)", "^/static/"};
std::cout << routes.size() << "\n"; // 3
std::cout << routes.is_match("/api/users") << "\n"; // 1 -- any-match, stops at the first hit
for (const std::size_t i : routes.which("/static/app.json")) {
std::cout << i << "\n"; // 1 then 2 -- construction order
}
See also#
The single-pattern engine each member is: basic_regex.
The same set from Python (
RegexSet): Python API.Multi-pattern benchmarks: Performance.
Migrating from
RE2::Set: the RE2 drop-in.