Categorygithub.com/dwisiswant0/pcregexp
modulepackage
0.1.0
Repository: https://github.com/dwisiswant0/pcregexp.git
Documentation: pkg.go.dev

# README

pcregexp

Tests Go Reference

pcregexp is a drop‑in replacement for Go's standard regexp package that uses the full capabilities of PCRE2 by loading the shared library dynamically at runtime, which enables cross‑compilation without the need for a C compiler (no Cgo required!). The API closely mirrors that of the standard library's regexp package while supporting advanced regex features like lookarounds and backreferences that PCRE2 provides.

[!WARNING] PCRE2 supports features that can lead to exponential runtime in some cases. Use pcregexp only with trusted regex patterns to avoid potential regular expression denial-of-service (ReDoS) issues (CWE-1333).

Requirements

  • go1.18 or later.
  • PCRE2 10.x shared library must be installed on your system.
  • Supported platforms:

Install

go install -v github.com/dwisiswant0/pcregexp@latest

Usage

package main

import (
    "fmt"
    
    "github.com/dwisiswant0/pcregexp"
)

func main() {
    // Compile a pattern. (Panics on error)
    re := pcregexp.MustCompile("p([a-z]+)ch")
    defer re.Close()

    // Check if the string matches the pattern.
    fmt.Println("MatchString(\"peach\"):", re.MatchString("peach"))

    // Find the leftmost match.
    fmt.Println("FindString(\"peach punch\"):", re.FindString("peach punch"))

    // Get the start and end indexes of the match.
    fmt.Println("FindStringIndex(\"peach punch\"):", re.FindStringIndex("peach punch"))

    // Retrieve the match along with its captured submatch.
    fmt.Println("FindStringSubmatch(\"peach punch\"):", re.FindStringSubmatch("peach punch"))

    // Perform global replacement (naively replaces all non-overlapping matches).
    src := "peach punch pinch"
    repl := "<fruit>"
    fmt.Println("ReplaceAllString:", re.ReplaceAllString(src, repl))
}

Wrapped Regexp API

Go Reference

You may want to use the regexp package provided here, which wraps both Go's standard regexp package and a PCRE2-based implementation, pcregexp. This unified interface automatically selects the appropriate engine based on the regex features used, offering the best of both worlds.

Benchmark

Execute the performance benchmark by running:

make bench

TODO

  • Implement PCRE2 JIT compilation support
    • Use native PCRE2 API JIT functions for improved performance
    • Add JIT compilation options and configurations
    • Implement memory management for JIT-compiled patterns
  • Implement these methods:
    • NumSubexp
    • LiteralPrefix
    • Longest
    • SubexpNames
    • SubexpIndex

Status

[!CAUTION] pcregexp has NOT reached 1.0 yet. Therefore, this library is currently not supported and does not offer a stable API; use at your own risk.

There are no guarantees of stability for the APIs in this library, and while they are not expected to change dramatically. API tweaks and bug fixes may occur.

License

pcregexp is released by @dwisiswant0 under the Apache 2.0 license. See LICENSE.

# Packages

No description provided by the author
No description provided by the author

# Functions

Compile compiles the given pattern and returns a [PCREgexp].
MustCompile is like Compile but panics on error.

# Structs

No description provided by the author