Skip to content
zig-utilsPublic

About

A modern, performant regular expression library for Zig.

Topics

Resources

Stars

33 stars

Watchers

0 watching

Forks

Repository files navigation

zig-regex

A modern, high-performance regular expression library for Zig

Zig License

Features - Installation - Quick Start - CLI - Documentation


Overview

zig-regex is a regular expression engine for Zig featuring Thompson NFA construction with linear time complexity, a backtracking engine for advanced features, and extensive pattern support. Built with zero external dependencies and full memory control through Zig allocators.

Features

Core Regex Features

Feature Syntax Status
Literals abc, 123 Stable
Quantifiers *, +, ?, {n}, {m,}, {m,n}, {,n} (= {0,n}, as in PCRE2 10.43+, Perl and YARA; literal under ECMAScript) Stable
Alternation a|b|c Stable
Character Classes \d, \w, \s, \D, \W, \S Stable
Custom Classes [abc], [a-z], [^0-9] Stable
Anchors ^, $, \b, \B Stable
Wildcards . Stable
Capturing Groups (...) Stable
Named Groups (?P<name>...), (?<name>...) Stable
Non-capturing (?:...) Stable
Lookahead (?=...), (?!...) Stable
Lookbehind (?<=...), (?<!...) Stable
Atomic groups (?>...) Stable
Backreferences \1, \2, \k<name>, (?P=name) Stable
Case-insensitive compileWithFlags(..., .{.case_insensitive = true}) Stable
Multiline compileWithFlags(..., .{.multiline = true}) Stable
Dot-all compileWithFlags(..., .{.dot_matches_newline = true}) Stable
Escaping \\, \., \n, \t, \r Stable

Advanced Features

  • Hybrid Execution Engine: Automatically selects between Thompson NFA (O(n*m)) and optimized backtracking
  • AST Optimization: Constant folding, dead code elimination, quantifier simplification
  • NFA Optimization: Epsilon transition removal, state merging, transition optimization
  • Pattern Macros: Composable, reusable pattern definitions
  • Type-Safe Builder API: Fluent interface for programmatic pattern construction
  • Thread Safety: Safe concurrent matching with SharedRegex and RegexCache
  • Pattern Analysis: Built-in ReDoS detection and pattern linting
  • Comprehensive API: compile, find, findAll, replace, replaceAll, split, iterator support

Quality

  • Zero Dependencies: Only Zig standard library
  • Linear Time Matching: Thompson NFA guarantees O(n*m) worst-case
  • Memory Safety: Full control via Zig allocators, no hidden allocations, zero leaks
  • 500+ Tests: Comprehensive test suite covering core features, edge cases, regressions, and stress tests

Installation

Using Zig Package Manager

Add to your build.zig.zon:

.dependencies = .{
    .regex = .{
        .url = "https://github.com/zig-utils/zig-regex/archive/main.tar.gz",
        .hash = "...", // zig will provide this
    },
},

Then in build.zig:

const regex = b.dependency("regex", .{
    .target = target,
    .optimize = optimize,
});
exe.root_module.addImport("regex", regex.module("regex"));

Manual Installation

git clone https://github.com/zig-utils/zig-regex.git
cd zig-regex
zig build
zig build test

Quick Start

Basic Pattern Matching

const std = @import("std");
const Regex = @import("regex").Regex;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var regex = try Regex.compile(allocator, "\\d{3}-\\d{4}");
    defer regex.deinit();

    if (try regex.find("Call me at 555-1234")) |match| {
        var mut_match = match;
        defer mut_match.deinit(allocator);
        std.debug.print("Found: {s}\n", .{match.slice}); // "555-1234"
    }
}

Find All Matches

var regex = try Regex.compile(allocator, "\\d+");
defer regex.deinit();

const matches = try regex.findAll(allocator, "a1b23c456");
defer {
    for (matches) |*m| {
        var mut_m = m;
        mut_m.deinit(allocator);
    }
    allocator.free(matches);
}

// matches: "1", "23", "456"

Replace

var regex = try Regex.compile(allocator, "(\\w+)@(\\w+)");
defer regex.deinit();

const result = try regex.replace(allocator, "email: user@host ok", "[$0]");
defer allocator.free(result);
// result: "email: [user@host] ok"

Capture Groups

var regex = try Regex.compile(allocator, "(\\d{4})-(\\d{2})-(\\d{2})");
defer regex.deinit();

if (try regex.find("Date: 2024-03-15")) |match| {
    var mut_match = match;
    defer mut_match.deinit(allocator);

    // match.captures[0] = "2024"
    // match.captures[1] = "03"
    // match.captures[2] = "15"
}

Case-Insensitive / Multiline

var regex = try Regex.compileWithFlags(allocator, "^hello", .{
    .case_insensitive = true,
    .multiline = true,
});
defer regex.deinit();

Multi-literal search

regex.literals holds the engines for searching many fixed byte strings at once, shared by zig-anti-virus and zig-waf. Patterns may contain any bytes, 0x00 and 0xff included, and every match reports the pattern's id. Each engine is built into a relocatable little-endian blob: write owned.bytes to disk, mmap it later and load it in place. Searching never allocates.

const lit = @import("regex").literals;

const patterns = [_]lit.Pattern{
    .{ .bytes = "select", .id = 1 },
    .{ .bytes = "\x00\xffMZ", .id = 2 },
};

// Teddy: SIMD search for up to 64 literals.
var teddy = try lit.Teddy.build(allocator, &patterns, .{ .ascii_case_insensitive = true });
defer teddy.deinit(allocator);
if (teddy.view.find(haystack, 0)) |m| std.debug.print("{d} at {d}..{d}\n", .{ m.pattern, m.start, m.end });

// Aho-Corasick: any number of literals, every match or leftmost-longest,
// and streaming over chunks with `scanStream`.
var ac = try lit.AhoCorasick.build(allocator, &patterns, .{});
defer ac.deinit(allocator);
var it = ac.view.iterator(haystack);
while (it.next()) |m| std.debug.print("{d} at {d}\n", .{ m.pattern, m.start });

// Sampled Bloom prefilter: candidate positions for very large sets. With
// stride 8 every anchor must be at least 11 bytes long.
var filter = try lit.Sbbf.build(allocator, &.{"\x4d\x5a\x90\x00\x03\x00\x00\x00\x04\x00\x00"}, .{ .stride = 8 });
defer filter.deinit(allocator);
var buf: [256]usize = undefined;
var probe = filter.view.prober(haystack, 0);
while (true) {
    const candidates = probe.next(&buf);
    if (candidates.len == 0) break;
    // An anchor may start in the 8 bytes ending at each candidate.
}

Byte Mode (binary data)

By default a pattern describes UTF-8 text: \xHH is the code point U+00HH (two bytes when HH >= 80) and . matches one code point. Set .bytes = true to match raw bytes instead, as PCRE does without its UTF option:

var sig = try Regex.compileWithFlags(allocator, "MZ.{2}\\x80\\xff[\\x00-\\x1f]+", .{ .bytes = true });
defer sig.deinit();
_ = try sig.isMatch(file_bytes);

In byte mode:

  • \xHH (one or two hex digits, none meaning NUL) and \x{H..} (up to FF) are one byte.
  • . matches any single byte; without dot_all it excludes \n only.
  • A raw byte in the pattern is its own atom, so \xc3\xa9+ repeats \xa9 only.
  • Character classes are byte sets ([\x80-\xff], [^a] matches one byte) and \s, \S, \d, \w and the POSIX classes are their ASCII forms.
  • case_insensitive folds ASCII letters only.
  • ^, $, \A, \z, \b and \B work as in UTF-8 mode; bytes >= 0x80 are not word bytes.
  • \p{..}, \P{..} and \u escapes, and the unicode, unicode_sets and ecmascript flags, are rejected with error.UnsupportedInBytesMode.

Allocation-Free Matching (Program and Scratch)

A Program is a compiled pattern flattened into one immutable byte blob, so a single program can be shared read-only by every thread. All mutable matching state (the lazy DFA cache, NFA thread lists) lives in a Scratch that each thread creates once and reuses. Matching never allocates.

const regex = @import("regex");

var program = try regex.Program.compile(allocator, "MZ.{2}\\x50\\x45", .{ .bytes = true });
defer program.deinit(allocator);

// Once per thread. `Scratch.initBuffer` takes caller memory instead.
var scratch = try regex.Scratch.init(allocator, &program);
defer scratch.deinit(allocator);

_ = try program.isMatch(&scratch, data);           // bool
if (try program.find(&scratch, data)) |span| {     // ?Span{ start, end }
    _ = data[span.start..span.end];
}
var it = program.iterator(&scratch, data);         // every match, no allocation
while (try it.next()) |span| _ = span;
var spans: [64]regex.program.Span = undefined;
const n = try program.findAll(&scratch, data, &spans); // error.NoSpaceLeft if more
_ = n;
  • Program.fromRegex(allocator, &re) flattens an already compiled Regex.

  • Programs cover every pattern the Thompson engine runs. Patterns that need the backtracking engine (backreferences, general lookaround, Unicode property escapes, and outside byte mode inline modifiers) and ECMAScript mode return error.UnsupportedByProgram. Program.compile drops capture groups and keeps lazy repeats and repeated alternations ((?:\x90|\xcc){4,}, a.+?b) on the linear engines; in byte mode it also builds inline modifiers ((?i), (?s:..), (?-i:..), (?m:..)) into the program. A standalone (?flags) lasts to the end of its group, later alternatives included, as in PCRE2 and Perl.

  • Results are spans without capture groups. Every match is the leftmost one; where it ends depends on the program's match kind, chosen when it is built and kept by bytes()/load():

    // PCRE2, Perl and YARA semantics: alternatives in order, greedy and lazy
    // repeats honoured (`a|ab` matches `a`, `a+?` one `a`).
    var pcre_like = try regex.Program.compileWithOptions(allocator, pattern, .{ .bytes = true }, .{ .match_kind = .leftmost_first });
    _ = pcre_like.matchKind(); // .leftmost_first

    The default, .leftmost_longest, extends the leftmost match as far as possible, as Regex.find does on the Thompson engines (on every path, its literal-alternation fast path included: a|ab finds ab). fromRegexWithOptions takes the same options. Leftmost-first programs agree with PCRE2 on 99.99% of randomly generated patterns; the rest are repeated groups whose iterations can match empty (see docs/BENCHMARKS.md).

  • One scratch can serve several programs: size it with Scratch.initCapacity(allocator, a.capacity().max(b.capacity())). A program larger than the scratch returns error.ScratchTooSmall.

  • A search runs forwards to the end of the leftmost match on a lazy DFA whose states are priority-ordered thread lists, then backwards over a reversed copy of the program to its start, so find, count and the iterator are linear in the input. While no thread is alive the forward scan skips to the next possible match start: a vector search for the rarest of the first 16 bytes of every match when that byte has at most 3 values (ASCII case-insensitive literals included), else a nibble-table search of the first-byte set. This works with assertions too, so \b and ^ patterns skip like any other.

  • When a pattern's DFA outgrows the scratch's state cache, the cache resets and that call finishes on an NFA simulation of the same program, still in time linear in the input.

Bounding memory and time

Hostile input cannot make a scratch grow or a search run unbounded:

var capacity = program.capacity();
capacity.dfa_states = 256;                  // DFA state cap (default 1024, 0 = NFA only)
var scratch = try regex.Scratch.initCapacity(allocator, capacity);
scratch.step_budget = 1_000_000;            // per search; null = unlimited

const hit = program.isMatch(&scratch, data) catch |err| switch (err) {
    error.BudgetExceeded => return .limit_event, // never reported as "no match"
    else => return err,
};
  • Scratch.bytesNeeded(capacity) is the exact size and is fixed when the scratch is made. For N states (capacity.states, which counts the reversed program's extra start state), C byte classes and a cap of D DFA states it is at most 56N + 4D(C+1) + 4D(W+9) + 4H + 16W + 40 bytes, with W = ceil(N/32) and H the smallest power of two that is at least 16 and at least 2D (Scratch.boundFor). With D = 256 that is at most 56N + 1024(C + W + 10) + 16W + 2088 bytes.
  • A step is one input byte a DFA reads (forwards or backwards) or skips, or one unit of NFA work. The budget applies to each isMatch, find and findFrom, and to each match an iterator, findAll or count looks for. scratch.steps reports what the last search used, never more than the budget.
  • The backtracking engine behind Regex has its own budget, re.setStepBudget(n) (default 10,000,000 steps); running out fails with error.Timeout.

Storing programs (serialisation)

A program's bytes are its serialised form: position independent (offsets, no pointers), little-endian, versioned and protected by a CRC-32. Write them into a file or a database section, then load them back without allocating:

const blob: []const u8 = program.bytes(); // store these bytes anywhere

// Later, for example over an mmapped region (any alignment):
const loaded = try regex.Program.load(mapped[offset..]); // borrows `mapped`
_ = try loaded.isMatch(&scratch, data);
// `loaded.bytes().len` is the program's length within the region;
// `loaded.dupe(allocator)` makes an owned copy.

Program.load checks the header, the version, the checksum and every structural invariant matching relies on (section layout, edge ranges and targets in both directions, byte-set indices, the byte-class map, the prefilter fields) and fails with error.Truncated, BadMagic, UnsupportedVersion, ChecksumMismatch or InvalidProgram, never with a fault. A loaded program borrows its bytes: they must outlive it and stay unchanged. The layout is version 2 (program.format_version); blobs written by 0.3.x are version 1 and fail with error.UnsupportedVersion, so rebuild stored programs from their patterns.

Backtracking programs (regex.bt)

Backreferences, lookahead, lookbehind, atomic groups and counted repeats too large to unroll are not regular, so a Program refuses them (error.UnsupportedByProgram, or ProgramTooLarge for x{50000,}). regex.bt compiles them into a bounded-backtracking program with the same guarantees: one relocatable, checksummed blob loaded in place without allocating, a Scratch sized once, no recursion, and a per-search step budget. Matching is PCRE's (leftmost-first, alternatives in order).

const bt = @import("regex").bt;

var sig = try bt.Program.compile(allocator, "(?P<k>[a-z]{4})=\\d+;(?P=k)(?!\\x00)", .{ .bytes = true });
defer sig.deinit(allocator);
var scratch = try bt.Scratch.init(allocator, &sig); // slots, counters, 16 Ki stack frames
defer scratch.deinit(allocator);
scratch.step_budget = 1_000_000;                    // default 10,000,000; always finite

const hit = sig.find(&scratch, data) catch |err| switch (err) {
    error.BudgetExceeded, error.StackExhausted => return .limit_event, // never "no match"
    else => return err,
};
_ = hit; // ?Span

const blob = sig.bytes();                   // store it; later:
const loaded = try bt.Program.load(blob);   // validated, borrowed, no allocation
_ = loaded;
  • Byte mode only. Counted repeats keep a counter (x{50000,} is a few instructions), and a run of a single byte set backtracks one byte at a time from one stack frame.
  • A repeated group stops after an iteration that matched empty, as in PCRE. A backreference inside its own group sees the previous iteration's capture. Lookbehind bodies need a bounded length (at most 65,535 bytes), variable lengths included.
  • Scratch.bytesNeeded is exact: 8 bytes per capture slot (three per backreferenced group), 16 per counter and 24 per stack frame (Capacity.frames, default 16,384). A full stack ends the search with error.StackExhausted.
  • load validates every opcode, jump target, set, slot and counter index and the pairing of lookaround and atomic markers; a forged blob can give wrong answers but cannot read out of bounds, and the step budget bounds even a forged loop.
  • benchmarks/pcre2/diff.sh with MODE=bt differential-tests these programs against PCRE2 (see docs/BENCHMARKS.md).

An atomic group whose body always matches one length ((?>abc), (?>[a-f]{4})) has a single end, so Regex and Program run it as a plain group; any other atomic group needs the backtracker (Regex) or regex.bt.

Required Literal Factors (prefilters)

requiredFactors reports literal strings every match must contain, so a prefilter can skip the regex entirely when none of them is in the input:

if (try regex.requiredFactors(allocator, "foo(bar|baz)qux", .{}, .{ .min_len = 3 })) |found| {
    var f = found;
    defer f.deinit(allocator);
    // f.prefix = "fooba" (every match starts with it), f.suffix = "qux",
    // f.inner = factors found elsewhere in every match.
}
// Same analysis for a compiled regex: try re.requiredFactors(allocator, .{}).
  • Each Factor has bytes and case_folded; a folded factor is lowercase and must be searched for ignoring ASCII case (from the case_insensitive flag, (?i:...), or a class such as [Ss]).
  • Factors are sound: every match contains them where they say. Alternation keeps what its branches share (bar|baz contributes ba), and anything that can repeat zero times or match an unknown character breaks a factor.
  • In byte mode the factors are raw bytes (\x80\xff gives those two bytes).
  • The result is null when no factor of at least min_len bytes (default 3) exists, as for (a|b)*.

CLI

zig-regex includes a command-line tool:

# Find first match
regex '\d+' 'hello 123 world'
# Output: 123

# Find all matches
regex -g '\d+' 'hello 123 world 456'
# Output
# 123
# 456

# Replace
regex -r '[$0]' '\d+' 'hello 123 world'
# Output: hello [123] world

# Case-insensitive
regex -i 'hello' 'HELLO world'

# Read from stdin
echo "hello 123 world" | regex '\d+'

# Version
regex -v

Building

zig build           # Build library and CLI
zig build test      # Run all tests
zig build run       # Run CLI
zig build example   # Run basic example
zig build bench     # Run benchmarks

Documentation

Requirements

  • Zig 0.17.0-dev.1857 or later
  • No external dependencies

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass (zig build test)
  5. Submit a pull request

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the zig-utils Discord Server

License

MIT License - see LICENSE file for details.

Acknowledgments

Inspired by:

  • Ken Thompson's NFA construction algorithm
  • RE2 (Google's regex engine)
  • Rust's regex crate

Support

About

A modern, performant regular expression library for Zig.

Topics

Resources

Stars

33 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages