← Interactive API Explorer
Namespace: System.Text.RegularExpressions

Regex

Represents an immutable, thread-safe regular expression engine guaranteed to execute in linear time.

âš¡ Linear-Time Engine

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
Dependencies & Link Setup:
Zero External Dependencies: Built directly into core 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

Regex(String pattern, RegexOptions options = RegexOptions::None)

Initializes a new instance of the Regex class for the specified regular expression pattern and optional matching options.

Static Methods

static bool IsMatch(String input, String pattern, RegexOptions options = RegexOptions::None)

Indicates whether the specified regular expression finds a match in the specified input string.

static Match Match(String input, String pattern, RegexOptions options = RegexOptions::None)

Searches the specified input string for the first occurrence of the specified regular expression.

static MatchCollection Matches(String input, String pattern, RegexOptions options = RegexOptions::None)

Searches the specified input string for all occurrences of a regular expression.

static String Replace(String input, String pattern, String replacement, RegexOptions options = RegexOptions::None)

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.

static Array<String> Split(String input, String pattern, RegexOptions options = RegexOptions::None)

Splits an input string into an array of substrings at the positions defined by a regular expression pattern.

static String Escape(String str)

Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $, ., #, and white space) by replacing them with their escape codes.

Instance Methods

bool IsMatch(String input)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.

Match Match(String input)

Searches the specified input string for the first occurrence of the regular expression specified in the constructor.

MatchCollection Matches(String input)

Searches the specified input string for all occurrences of the regular expression.

String Replace(String input, String replacement)

In a specified input string, replaces all strings that match the regular expression pattern with a specified replacement string.

Array<String> Split(String input)

Splits an input string into an array of substrings at the positions defined by the regular expression pattern.

String ToString() const

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;
}