Documentation
ΒΆ
Overview ΒΆ
Package coregex provides a high-performance regex engine for Go.
coregex achieves 5-50x speedup over Go's stdlib regexp through:
- Multi-engine architecture (NFA, Lazy DFA, prefilters)
- SIMD-accelerated primitives (memchr, memmem, teddy)
- Literal extraction and prefiltering
- Automatic strategy selection
The public API is compatible with stdlib regexp where possible, making it easy to migrate existing code.
Basic usage:
// Compile a pattern
re, err := coregex.Compile(`\d+`)
if err != nil {
log.Fatal(err)
}
// Find first match
match := re.Find([]byte("hello 123 world"))
fmt.Println(string(match)) // "123"
// Check if matches
if re.Match([]byte("hello 123")) {
fmt.Println("matched!")
}
Advanced usage:
// Custom configuration
config := coregex.DefaultConfig()
config.MaxDFAStates = 50000
re, err := coregex.CompileWithConfig("(a|b|c)*", config)
Performance characteristics:
- Patterns with literals: 5-50x faster (prefilter optimization)
- Simple patterns: comparable to stdlib
- Complex patterns: 2-10x faster (DFA avoids backtracking)
- Worst case: guaranteed O(m*n) (ReDoS safe)
Limitations (v1.0):
- No capture groups (coming in v1.1)
- No replace functions (coming in v1.1)
- No multiline/case-insensitive flags (coming in v1.1)
Index ΒΆ
- func DefaultConfig() meta.Config
- type Regex
- func (r *Regex) Find(b []byte) []byte
- func (r *Regex) FindAll(b []byte, n int) [][]byte
- func (r *Regex) FindAllString(s string, n int) []string
- func (r *Regex) FindIndex(b []byte) []int
- func (r *Regex) FindString(s string) string
- func (r *Regex) FindStringIndex(s string) []int
- func (r *Regex) Match(b []byte) bool
- func (r *Regex) MatchString(s string) bool
- func (r *Regex) String() string
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func DefaultConfig ΒΆ
DefaultConfig returns the default configuration for compilation.
Users can customize this and pass to CompileWithConfig.
Example:
config := coregex.DefaultConfig()
config.EnableDFA = false // Use NFA only
re, _ := coregex.CompileWithConfig("pattern", config)
Types ΒΆ
type Regex ΒΆ
type Regex struct {
// contains filtered or unexported fields
}
Regex represents a compiled regular expression.
A Regex is safe to use concurrently from multiple goroutines, except for methods that modify internal state (like ResetStats).
Example:
re := coregex.MustCompile(`hello`)
if re.Match([]byte("hello world")) {
println("matched!")
}
func Compile ΒΆ
Compile compiles a regular expression pattern.
Syntax is Perl-compatible (same as Go's stdlib regexp). Returns an error if the pattern is invalid.
Example:
re, err := coregex.Compile(`\d{3}-\d{4}`)
if err != nil {
log.Fatal(err)
}
Example ΒΆ
ExampleCompile demonstrates basic pattern compilation and matching.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re, err := coregex.Compile(`\d+`)
if err != nil {
panic(err)
}
fmt.Println(re.Match([]byte("hello 123")))
}
Output: true
func CompileWithConfig ΒΆ
CompileWithConfig compiles a pattern with custom configuration.
This allows fine-tuning of performance characteristics.
Example:
config := coregex.DefaultConfig()
config.MaxDFAStates = 100000 // Larger cache
re, err := coregex.CompileWithConfig("(a|b|c)*", config)
Example ΒΆ
ExampleCompileWithConfig demonstrates custom configuration.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
config := coregex.DefaultConfig()
config.MaxDFAStates = 50000 // Increase cache size
re, err := coregex.CompileWithConfig("(a|b|c)*", config)
if err != nil {
panic(err)
}
fmt.Println(re.MatchString("abcabc"))
}
Output: true
func MustCompile ΒΆ
MustCompile compiles a regular expression pattern and panics if it fails.
This is useful for patterns known to be valid at compile time.
Example:
var emailRegex = coregex.MustCompile(`[a-z]+@[a-z]+\.[a-z]+`)
Example ΒΆ
ExampleMustCompile demonstrates panic-on-error compilation.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re := coregex.MustCompile(`hello`)
fmt.Println(re.MatchString("hello world"))
}
Output: true
func (*Regex) Find ΒΆ
Find returns a slice holding the text of the leftmost match in b. Returns nil if no match is found.
Example:
re := coregex.MustCompile(`\d+`)
match := re.Find([]byte("age: 42"))
println(string(match)) // "42"
Example ΒΆ
ExampleRegex_Find demonstrates finding the first match.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re := coregex.MustCompile(`\d+`)
match := re.Find([]byte("age: 42 years"))
fmt.Println(string(match))
}
Output: 42
func (*Regex) FindAll ΒΆ
FindAll returns a slice of all successive matches of the pattern in b. If n > 0, it returns at most n matches. If n <= 0, it returns all matches.
Example:
re := coregex.MustCompile(`\d+`)
matches := re.FindAll([]byte("1 2 3"), -1)
// matches = [[]byte("1"), []byte("2"), []byte("3")]
Example ΒΆ
ExampleRegex_FindAll demonstrates finding all matches.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re := coregex.MustCompile(`\d`)
matches := re.FindAll([]byte("a1b2c3"), -1)
for _, m := range matches {
fmt.Print(string(m), " ")
}
fmt.Println()
}
Output: 1 2 3
func (*Regex) FindAllString ΒΆ
FindAllString returns a slice of all successive matches of the pattern in s. If n > 0, it returns at most n matches. If n <= 0, it returns all matches.
Example:
re := coregex.MustCompile(`\d+`)
matches := re.FindAllString("1 2 3", -1)
// matches = ["1", "2", "3"]
Example ΒΆ
ExampleRegex_FindAllString demonstrates finding all string matches.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re := coregex.MustCompile(`\w+`)
words := re.FindAllString("hello world test", -1)
for _, word := range words {
fmt.Print(word, " ")
}
fmt.Println()
}
Output: hello world test
func (*Regex) FindIndex ΒΆ
FindIndex returns a two-element slice of integers defining the location of the leftmost match in b. The match is at b[loc[0]:loc[1]]. Returns nil if no match is found.
Example:
re := coregex.MustCompile(`\d+`)
loc := re.FindIndex([]byte("age: 42"))
println(loc[0], loc[1]) // 5, 7
Example ΒΆ
ExampleRegex_FindIndex demonstrates finding match positions.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re := coregex.MustCompile(`\d+`)
loc := re.FindIndex([]byte("age: 42"))
fmt.Printf("Match at [%d:%d]\n", loc[0], loc[1])
}
Output: Match at [5:7]
func (*Regex) FindString ΒΆ
FindString returns a string holding the text of the leftmost match in s. Returns empty string if no match is found.
Example:
re := coregex.MustCompile(`\d+`)
match := re.FindString("age: 42")
println(match) // "42"
Example ΒΆ
ExampleRegex_FindString demonstrates finding a match in a string.
package main
import (
"fmt"
"github.com/coregx/coregex"
)
func main() {
re := coregex.MustCompile(`\w+@\w+\.\w+`)
email := re.FindString("Contact: user@example.com")
fmt.Println(email)
}
Output: user@example.com
func (*Regex) FindStringIndex ΒΆ
FindStringIndex returns a two-element slice of integers defining the location of the leftmost match in s. The match is at s[loc[0]:loc[1]]. Returns nil if no match is found.
Example:
re := coregex.MustCompile(`\d+`)
loc := re.FindStringIndex("age: 42")
println(loc[0], loc[1]) // 5, 7
func (*Regex) Match ΒΆ
Match reports whether the byte slice b contains any match of the pattern.
Example:
re := coregex.MustCompile(`\d+`)
if re.Match([]byte("hello 123")) {
println("contains digits")
}
func (*Regex) MatchString ΒΆ
MatchString reports whether the string s contains any match of the pattern.
Example:
re := coregex.MustCompile(`hello`)
if re.MatchString("hello world") {
println("matched!")
}
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
dfa
|
|
|
lazy
Package lazy implements a Lazy DFA (Deterministic Finite Automaton) engine for regex matching.
|
Package lazy implements a Lazy DFA (Deterministic Finite Automaton) engine for regex matching. |
|
internal
|
|
|
sparse
Package sparse provides a sparse set data structure for efficient membership testing.
|
Package sparse provides a sparse set data structure for efficient membership testing. |
|
Package literal provides types and operations for extracting literal sequences from regex patterns for prefilter optimization.
|
Package literal provides types and operations for extracting literal sequences from regex patterns for prefilter optimization. |
|
Package meta implements the meta-engine orchestrator that automatically selects the optimal regex execution strategy.
|
Package meta implements the meta-engine orchestrator that automatically selects the optimal regex execution strategy. |
|
Package nfa provides a Thompson NFA (Non-deterministic Finite Automaton) implementation for regex matching.
|
Package nfa provides a Thompson NFA (Non-deterministic Finite Automaton) implementation for regex matching. |
|
Package prefilter provides fast candidate filtering for regex search using extracted literal sequences.
|
Package prefilter provides fast candidate filtering for regex search using extracted literal sequences. |
|
Package simd provides SIMD-accelerated string operations for high-performance byte searching.
|
Package simd provides SIMD-accelerated string operations for high-performance byte searching. |