coregx/coregex

Pure Go production-grade regex engine with SIMD optimizations. Up to 3-3000x+ faster than stdlib.

Go

260

269 commits

updated Sep 6, 2026

See the code

README

coregex

GitHub Release Go Version Go Reference CI Go Report Card codecov License GitHub Stars GitHub Issues GitHub Discussions Financial Contributors on Open Collective

High-performance regex engine for Go. Drop-in replacement for regexp with 3-3000x speedup.*

* Typical speedup 15-240x on real-world patterns. 1000x+ achieved on specific edge cases where prefilters skip entire input (e.g., IP pattern on text with no digits).

Why coregex?

Go's stdlib regexp is intentionally simple — single NFA engine, no optimizations. This guarantees O(n) time but leaves performance on the table.

coregex brings Rust regex-crate architecture to Go:

  • Multi-engine: 17 strategies — Lazy DFA, PikeVM, OnePass, BoundedBacktracker, and more
  • SIMD prefilters: AVX2/SSSE3 for fast candidate rejection
  • Reverse search: Suffix/inner literal patterns run 1000x+ faster
  • O(n) guarantee: No backtracking, no ReDoS vulnerabilities

Installation

go get github.com/coregx/coregex

Requires Go 1.25+. Minimal dependencies (golang.org/x/sys, github.com/coregx/ahocorasick).

Quick Start

package main

import (
    "fmt"
    "github.com/coregx/coregex"
)

func main() {
    re := coregex.MustCompile(`\w+@\w+\.\w+`)

    text := []byte("Contact support@example.com for help")

    // Find first match
    fmt.Printf("Found: %s\n", re.Find(text))

    // Check if matches (zero allocation)
    if re.MatchString("test@email.com") {
        fmt.Println("Valid email format")
    }
}

Performance

Cross-language benchmarks on 6MB input, AMD EPYC (source):

PatternGo stdlibcoregexRust regexvs stdlibvs Rust
Inner .*keyword.*232 ms0.26 ms13.6 ms893x52x faster
IP address489 ms0.77 ms13.5 ms635x17.6x faster
Email validation257 ms0.55 ms0.26 ms467x2.1x slower
URL extraction258 ms0.61 ms0.34 ms424x1.8x slower
Multiline (?m)^/.*\.php101 ms0.38 ms0.76 ms266x2.0x faster
Version \d+.\d+.\d+163 ms0.65 ms0.79 ms250x1.2x faster
Suffix .*\.txt236 ms1.79 ms13.7 ms132x7.7x faster
HTTP methods103 ms1.51 ms0.64 ms68x2.4x slower
Literal alternation232 ms4.69 ms0.63 ms49x7.4x slower
Multi-literal236 ms12.9 ms5.3 ms18x2.4x slower
Char class [\w]+507 ms41.9 ms58.4 ms12x1.4x faster
Word repeat (\w{2,8})+647 ms179 ms56 ms3.6x3.2x slower

Where coregex excels:

  • Inner literals (.*error.*) — bidirectional DFA, 52x faster than Rust
  • IP/phone patterns (\d+\.\d+\.\d+\.\d+) — SIMD digit prefilter, 17.6x faster than Rust
  • Suffix patterns (.*\.log, .*\.txt) — reverse search, 7.7x faster than Rust
  • Multiline patterns ((?m)^/.*\.php) — 2.0x faster than Rust, 266x vs stdlib
  • Multi-pattern (foo|bar|baz|...) — Slim Teddy (≤32), Fat Teddy (33-64), or Aho-Corasick (>64)
  • Anchored alternations (^(\d+|UUID|hex32)) — O(1) branch dispatch (5-20x)
  • Concatenated char classes ([a-zA-Z]+[0-9]+) — DFA with byte classes (5-7x)
  • Zero-alloc iterators (AllIndex, AppendAllIndex) — 0 heap allocs, up to 30% faster than FindAll. Email pattern faster than Rust with AppendAllIndex.

Features

Engine Selection

coregex automatically selects the optimal engine:

StrategyPattern TypeSpeedup
AnchoredLiteral^prefix.*suffix$32-133x
MultilineReverseSuffix(?m)^/.*\.php100-552x
ReverseInner.*keyword.*100-900x
ReverseSuffix.*\.txt100-1100x
BranchDispatch^(\d+|UUID|hex32)5-20x
CompositeSequenceDFA[a-zA-Z]+[0-9]+5-7x
LazyDFAIP, complex patterns10-150x
AhoCorasicka|b|c|...|z (>64 patterns)75-113x
CharClassSearcher[\w]+, \d+4-25x
Slim Teddyfoo|bar|baz (2-32 patterns)15-240x
Fat Teddy33-64 patterns60-73x
OnePassAnchored captures10x
BoundedBacktrackerSmall patterns2-5x

API Compatibility

Drop-in replacement for regexp.Regexp:

// stdlib
re := regexp.MustCompile(pattern)

// coregex — same API
re := coregex.MustCompile(pattern)

Supported methods:

  • Match, MatchString, MatchReader
  • Find, FindString, FindAll, FindAllString
  • FindIndex, FindStringIndex, FindAllIndex
  • FindSubmatch, FindStringSubmatch, FindAllSubmatch
  • ReplaceAll, ReplaceAllString, ReplaceAllFunc
  • Split, SubexpNames, NumSubexp
  • Longest, Copy, String

Zero-Allocation APIs

// Zero allocations — boolean match
matched := re.IsMatch(text)

// Zero allocations — single match indices
start, end, found := re.FindIndices(text)

// Zero allocations — iterator over all matches (Go 1.23+)
for m := range re.AllIndex(data) {
    fmt.Printf("match at [%d, %d]\n", m[0], m[1])
}

// Zero allocations — match content iterator
for s := range re.AllString(text) {
    fmt.Println(s)
}

// Buffer-reuse — append to caller's slice (strconv.Append* pattern)
var buf [][2]int
for _, chunk := range chunks {
    buf = re.AppendAllIndex(buf[:0], chunk, -1)
    process(buf)
}

Configuration

config := coregex.DefaultConfig()
config.DFAMaxStates = 10000      // Limit DFA cache
config.EnablePrefilter = true    // SIMD acceleration

re, err := coregex.CompileWithConfig(pattern, config)

Thread Safety

A compiled *Regexp is safe for concurrent use by multiple goroutines:

re := coregex.MustCompile(`\d+`)

// Safe: multiple goroutines sharing one compiled pattern
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        re.FindString("test 123 data")  // thread-safe
    }()
}
wg.Wait()

Internally uses sync.Pool (same pattern as Go stdlib regexp) for per-search state management.

Syntax Support

Uses Go's regexp/syntax parser:

FeatureSupport
Character classes[a-z], \d, \w, \s
Quantifiers*, +, ?, {n,m}
Anchors^, $, \b, \B
Groups(...), (?:...), (?P<name>...)
Unicode\p{L}, \P{N}
Flags(?i), (?m), (?s)
BackreferencesNot supported (O(n) guarantee)

Architecture

Pattern → Parse → NFA → Literal Extract → Strategy Select
                                               ↓
                  ┌────────────────────────────────────────────┐
                  │ Engines (17 strategies):                   │
                  │  LazyDFA, PikeVM, OnePass,                 │
                  │  BoundedBacktracker, ReverseAnchored,      │
                  │  ReverseInner, ReverseSuffix,              │
                  │  ReverseSuffixSet, MultilineReverseSuffix, │
                  │  AnchoredLiteral, CharClassSearcher,       │
                  │  Teddy, DigitPrefilter, AhoCorasick,       │
                  │  CompositeSearcher, BranchDispatch, Both   │
                  └────────────────────────────────────────────┘
                                               ↓
Input → Prefilter (SIMD) → Engine → Match Result

For detailed architecture documentation, see docs/ARCHITECTURE.md. For optimization details, see docs/OPTIMIZATIONS.md.

SIMD Primitives (AMD64):

  • memchr — single byte search (AVX2)
  • memmem — substring search (SSSE3)
  • Slim Teddy — multi-pattern search, 2-32 patterns (SSSE3, 9+ GB/s)
  • Fat Teddy — multi-pattern search, 33-64 patterns (AVX2, 9+ GB/s)

Pure Go fallback on other architectures.

Battle-Tested

coregex was tested in GoAWK. This real-world testing uncovered 15+ edge cases that synthetic benchmarks missed.

Powered by coregex: uawk

uawk is a modern AWK interpreter built on coregex:

Benchmark (10MB)GoAWKuawkSpeedup
Regex alternation1.85s97ms19x
IP matching290ms99ms2.9x
General regex320ms100ms3.2x
go install github.com/kolkov/uawk/cmd/uawk@latest
uawk '/error/ { print $0 }' server.log

We need more testers! If you have a project using regexp, try coregex and report issues.

Documentation

Comparison

coregexstdlibregexp2
Performance3-3000x fasterBaselineSlower
SIMDAVX2/SSSE3NoNo
O(n) guaranteeYesYesNo
BackreferencesNoNoYes
APIDrop-inDifferent

Use coregex for performance-critical code with O(n) guarantee. Use stdlib for simple cases where performance doesn't matter. Use regexp2 if you need backreferences (accept exponential worst-case).

Inspired by:

Sponsors

coregex is an independent open-source project. Development is sustained by contributors and sponsors.

Sponsors

Backers

Backers

Sponsor on Open Collective

License

MIT — see LICENSE.


Status: Pre-1.0 (API may change). Ready for testing and feedback.

Releases · Issues · Discussions

Star History

Star History Chart
avx2
dfa
go
golang
multi-engine
nfa
performance
pikevm
regex
regex-engine
regexp
simd
ssse3

Contributors

kolkov

268 commits

benhoyt

1 commits

coregx/coregex

Pure Go production-grade regex engine with SIMD optimizations. Up to 3-3000x+ faster than stdlib.

Go

260

269 commits

updated Sep 6, 2026

See the code

README

coregex

GitHub Release Go Version Go Reference CI Go Report Card codecov License GitHub Stars GitHub Issues GitHub Discussions Financial Contributors on Open Collective

High-performance regex engine for Go. Drop-in replacement for regexp with 3-3000x speedup.*

* Typical speedup 15-240x on real-world patterns. 1000x+ achieved on specific edge cases where prefilters skip entire input (e.g., IP pattern on text with no digits).

Why coregex?

Go's stdlib regexp is intentionally simple — single NFA engine, no optimizations. This guarantees O(n) time but leaves performance on the table.

coregex brings Rust regex-crate architecture to Go:

  • Multi-engine: 17 strategies — Lazy DFA, PikeVM, OnePass, BoundedBacktracker, and more
  • SIMD prefilters: AVX2/SSSE3 for fast candidate rejection
  • Reverse search: Suffix/inner literal patterns run 1000x+ faster
  • O(n) guarantee: No backtracking, no ReDoS vulnerabilities

Installation

go get github.com/coregx/coregex

Requires Go 1.25+. Minimal dependencies (golang.org/x/sys, github.com/coregx/ahocorasick).

Quick Start

package main

import (
    "fmt"
    "github.com/coregx/coregex"
)

func main() {
    re := coregex.MustCompile(`\w+@\w+\.\w+`)

    text := []byte("Contact support@example.com for help")

    // Find first match
    fmt.Printf("Found: %s\n", re.Find(text))

    // Check if matches (zero allocation)
    if re.MatchString("test@email.com") {
        fmt.Println("Valid email format")
    }
}

Performance

Cross-language benchmarks on 6MB input, AMD EPYC (source):

PatternGo stdlibcoregexRust regexvs stdlibvs Rust
Inner .*keyword.*232 ms0.26 ms13.6 ms893x52x faster
IP address489 ms0.77 ms13.5 ms635x17.6x faster
Email validation257 ms0.55 ms0.26 ms467x2.1x slower
URL extraction258 ms0.61 ms0.34 ms424x1.8x slower
Multiline (?m)^/.*\.php101 ms0.38 ms0.76 ms266x2.0x faster
Version \d+.\d+.\d+163 ms0.65 ms0.79 ms250x1.2x faster
Suffix .*\.txt236 ms1.79 ms13.7 ms132x7.7x faster
HTTP methods103 ms1.51 ms0.64 ms68x2.4x slower
Literal alternation232 ms4.69 ms0.63 ms49x7.4x slower
Multi-literal236 ms12.9 ms5.3 ms18x2.4x slower
Char class [\w]+507 ms41.9 ms58.4 ms12x1.4x faster
Word repeat (\w{2,8})+647 ms179 ms56 ms3.6x3.2x slower

Where coregex excels:

  • Inner literals (.*error.*) — bidirectional DFA, 52x faster than Rust
  • IP/phone patterns (\d+\.\d+\.\d+\.\d+) — SIMD digit prefilter, 17.6x faster than Rust
  • Suffix patterns (.*\.log, .*\.txt) — reverse search, 7.7x faster than Rust
  • Multiline patterns ((?m)^/.*\.php) — 2.0x faster than Rust, 266x vs stdlib
  • Multi-pattern (foo|bar|baz|...) — Slim Teddy (≤32), Fat Teddy (33-64), or Aho-Corasick (>64)
  • Anchored alternations (^(\d+|UUID|hex32)) — O(1) branch dispatch (5-20x)
  • Concatenated char classes ([a-zA-Z]+[0-9]+) — DFA with byte classes (5-7x)
  • Zero-alloc iterators (AllIndex, AppendAllIndex) — 0 heap allocs, up to 30% faster than FindAll. Email pattern faster than Rust with AppendAllIndex.

Features

Engine Selection

coregex automatically selects the optimal engine:

StrategyPattern TypeSpeedup
AnchoredLiteral^prefix.*suffix$32-133x
MultilineReverseSuffix(?m)^/.*\.php100-552x
ReverseInner.*keyword.*100-900x
ReverseSuffix.*\.txt100-1100x
BranchDispatch^(\d+|UUID|hex32)5-20x
CompositeSequenceDFA[a-zA-Z]+[0-9]+5-7x
LazyDFAIP, complex patterns10-150x
AhoCorasicka|b|c|...|z (>64 patterns)75-113x
CharClassSearcher[\w]+, \d+4-25x
Slim Teddyfoo|bar|baz (2-32 patterns)15-240x
Fat Teddy33-64 patterns60-73x
OnePassAnchored captures10x
BoundedBacktrackerSmall patterns2-5x

API Compatibility

Drop-in replacement for regexp.Regexp:

// stdlib
re := regexp.MustCompile(pattern)

// coregex — same API
re := coregex.MustCompile(pattern)

Supported methods:

  • Match, MatchString, MatchReader
  • Find, FindString, FindAll, FindAllString
  • FindIndex, FindStringIndex, FindAllIndex
  • FindSubmatch, FindStringSubmatch, FindAllSubmatch
  • ReplaceAll, ReplaceAllString, ReplaceAllFunc
  • Split, SubexpNames, NumSubexp
  • Longest, Copy, String

Zero-Allocation APIs

// Zero allocations — boolean match
matched := re.IsMatch(text)

// Zero allocations — single match indices
start, end, found := re.FindIndices(text)

// Zero allocations — iterator over all matches (Go 1.23+)
for m := range re.AllIndex(data) {
    fmt.Printf("match at [%d, %d]\n", m[0], m[1])
}

// Zero allocations — match content iterator
for s := range re.AllString(text) {
    fmt.Println(s)
}

// Buffer-reuse — append to caller's slice (strconv.Append* pattern)
var buf [][2]int
for _, chunk := range chunks {
    buf = re.AppendAllIndex(buf[:0], chunk, -1)
    process(buf)
}

Configuration

config := coregex.DefaultConfig()
config.DFAMaxStates = 10000      // Limit DFA cache
config.EnablePrefilter = true    // SIMD acceleration

re, err := coregex.CompileWithConfig(pattern, config)

Thread Safety

A compiled *Regexp is safe for concurrent use by multiple goroutines:

re := coregex.MustCompile(`\d+`)

// Safe: multiple goroutines sharing one compiled pattern
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        re.FindString("test 123 data")  // thread-safe
    }()
}
wg.Wait()

Internally uses sync.Pool (same pattern as Go stdlib regexp) for per-search state management.

Syntax Support

Uses Go's regexp/syntax parser:

FeatureSupport
Character classes[a-z], \d, \w, \s
Quantifiers*, +, ?, {n,m}
Anchors^, $, \b, \B
Groups(...), (?:...), (?P<name>...)
Unicode\p{L}, \P{N}
Flags(?i), (?m), (?s)
BackreferencesNot supported (O(n) guarantee)

Architecture

Pattern → Parse → NFA → Literal Extract → Strategy Select
                                               ↓
                  ┌────────────────────────────────────────────┐
                  │ Engines (17 strategies):                   │
                  │  LazyDFA, PikeVM, OnePass,                 │
                  │  BoundedBacktracker, ReverseAnchored,      │
                  │  ReverseInner, ReverseSuffix,              │
                  │  ReverseSuffixSet, MultilineReverseSuffix, │
                  │  AnchoredLiteral, CharClassSearcher,       │
                  │  Teddy, DigitPrefilter, AhoCorasick,       │
                  │  CompositeSearcher, BranchDispatch, Both   │
                  └────────────────────────────────────────────┘
                                               ↓
Input → Prefilter (SIMD) → Engine → Match Result

For detailed architecture documentation, see docs/ARCHITECTURE.md. For optimization details, see docs/OPTIMIZATIONS.md.

SIMD Primitives (AMD64):

  • memchr — single byte search (AVX2)
  • memmem — substring search (SSSE3)
  • Slim Teddy — multi-pattern search, 2-32 patterns (SSSE3, 9+ GB/s)
  • Fat Teddy — multi-pattern search, 33-64 patterns (AVX2, 9+ GB/s)

Pure Go fallback on other architectures.

Battle-Tested

coregex was tested in GoAWK. This real-world testing uncovered 15+ edge cases that synthetic benchmarks missed.

Powered by coregex: uawk

uawk is a modern AWK interpreter built on coregex:

Benchmark (10MB)GoAWKuawkSpeedup
Regex alternation1.85s97ms19x
IP matching290ms99ms2.9x
General regex320ms100ms3.2x
go install github.com/kolkov/uawk/cmd/uawk@latest
uawk '/error/ { print $0 }' server.log

We need more testers! If you have a project using regexp, try coregex and report issues.

Documentation

Comparison

coregexstdlibregexp2
Performance3-3000x fasterBaselineSlower
SIMDAVX2/SSSE3NoNo
O(n) guaranteeYesYesNo
BackreferencesNoNoYes
APIDrop-inDifferent

Use coregex for performance-critical code with O(n) guarantee. Use stdlib for simple cases where performance doesn't matter. Use regexp2 if you need backreferences (accept exponential worst-case).

Inspired by:

Sponsors

coregex is an independent open-source project. Development is sustained by contributors and sponsors.

Sponsors

Backers

Backers

Sponsor on Open Collective

License

MIT — see LICENSE.


Status: Pre-1.0 (API may change). Ready for testing and feedback.

Releases · Issues · Discussions

Star History

Star History Chart
avx2
dfa
go
golang
multi-engine
nfa
performance
pikevm
regex
regex-engine
regexp
simd
ssse3

Contributors

kolkov

268 commits

benhoyt

1 commits

Languages

Go

96.1%

Assembly

3.2%