Regex
Represents an immutable, thread-safe regular expression engine guaranteed to execute in linear time.
Native UTF-16 ReDoS-Immune Architecture (Zero External Dependencies)
Zero Dependencies
Unlike std::regex (libstdc++) and PCRE2, which utilize backtracking algorithms vulnerable to pathological exponential-time slowdowns (ReDoS attacks), SystemH's Regex is built on a high-performance 100% native UTF-16 PikeVM finite automata engine with zero external dependencies. It operates natively on char16_t buffers without UTF-8 transcoding overhead and guarantees execution time strictly linear in the input length ($O(N)$), ensuring 100% security against evil regex payloads on high-scale cloud servers and embedded targets (ESP32/STM32) alike.
| Feature / Characteristic | SystemH Regex (Native UTF-16) | std::regex (libstdc++) | .NET 10 CLR Regex |
|---|---|---|---|
| Time Complexity (Worst-Case) | $O(N)$ strictly linear | $O(2^N)$ exponential | Exponential without timeouts |
| Catastrophic Backtracking (ReDoS) | Immune by design | Vulnerable to hang / freeze | Requires MatchTimeout |
| Concurrent Match Safety | 100% thread-safe across ThreadPool | Thread-safe for read | Thread-safe |
| Unicode Character Indexing | Direct UTF-16 code units (No transcoding) | Byte or wchar depending on type | UTF-16 code units |
System.h with pure C++17.No external libraries (no RE2 or Abseil) required. Embedded & ESP32 ready out of the box with no extra linker flags.
Constructors
Initializes a new instance of the Regex class for the specified regular expression pattern and optional matching options.
Static Methods
Indicates whether the specified regular expression finds a match in the specified input string.
Searches the specified input string for the first occurrence of the specified regular expression.
Searches the specified input string for all occurrences of a regular expression.
In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string. Supports $1, $2, and $& capture backreferences.
Splits an input string into an array of substrings at the positions defined by a regular expression pattern.
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $, ., #, and white space) by replacing them with their escape codes.
Instance Methods
Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.
Searches the specified input string for the first occurrence of the regular expression specified in the constructor.
Searches the specified input string for all occurrences of the regular expression.
In a specified input string, replaces all strings that match the regular expression pattern with a specified replacement string.
Splits an input string into an array of substrings at the positions defined by the regular expression pattern.
Returns the regular expression pattern that was passed into the Regex constructor.
Code Example
#include "System.h"
using namespace System;
using namespace System::Text::RegularExpressions;
int main() {
// 1. Capture group extraction
Regex emailRegex(u"(\\w+)@(\\w+)\\.com");
Match match = emailRegex.Match(u"Contact: alice@example.com today");
if (match.Success) {
Console::WriteLine(u"User: " + match.Groups[1].Value); // "alice"
Console::WriteLine(u"Domain: " + match.Groups[2].Value); // "example"
Console::WriteLine(u"Index: " + String(match.Index)); // Char offset 9
}
// 2. Linear-time replacement with backreferences
Regex re(u"(\\w+)=(\\w+)");
String swapped = re.Replace(u"x=1, y=2", u"$2=$1"); // "1=x, 2=y"
Console::WriteLine(swapped);
return 0;
}