regex

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2025 License: MIT Imports: 10 Imported by: 3

README

Go Regex

A High Performance Regex Package That Uses A Cache.

After calling a regex, the compiled output gets cached to improve performance.

This package uses the core go regexp package under the hood, so its free from external dependencies.

Installation

go get github.com/tkdeng/regex

Usage

import (
  "github.com/tkdeng/regex"
)

func main(){
  // pre compile a regex into the cache
  regex.Comp(`re`)

  // compile a regex and safely escape user input
  regex.Comp(`re %1`, `this will be escaped .*`); // output: this will be escaped \.\*
  regex.Comp(`re %1`, `hello \n world`); // output: hello \\n world (note: the \ was escaped, and the n is literal)

  // use %n to reference a param
  // use %{n} for param indexes with more than 1 digit
  regex.Comp(`re %1 and %2 ... %{12}`, `param 1`, `param 2` ..., `param 12`);

  // return an error instead of panic on failed compile
  reg, err := regex.CompTry(`re`)


  // manually escape a string
  // note: the compile methods params are automatically escaped
  regex.Escape(`(.*)? \$ \\$ \\\$ regex hack failed`)

  // determine if a regex is valid, and can be compiled by this module
  regex.IsValid(`re`)

  // determine if a regex is valid, and can be compiled by the builtin RE2 Regexp module
  regex.IsValidRE2(`re`)

  // run a replace function (most advanced feature)
  regex.Comp(`(?flags)re(capture group)`).RepFunc(myByteArray, func(b func(int) []byte) []byte {
    b(0) // get the string
    b(1) // get the first capture group

    return []byte("")

    // returning nil will stop the loop early
    return nil
  })

  // replace with a string
  regex.Comp(`re (capture)`).Rep(myByteArray, []byte("test $1"))

  // replace with a string literal
  regex.Comp(`re`).RepLit(myByteArray, []byte("all capture groups ignored (ie: $1)"))


  // return a bool if a regex matches a byte array
  regex.Comp(`re`).Match(myByteArray)

  // split a byte array in a similar way to JavaScript
  regex.Comp(`re|(keep this and split like in JavaScript)`).Split(myByteArray) // [][]byte
  regex.Comp(`re|(keep this and split)`).SplitStr(myByteArray) // []string

  // a regex string is modified before compiling, to add a few other features
  `use \' in place of ` + "`" + ` to make things easier`
  `(?#This is a comment in regex)`

  // direct access to compiled *regexp.Regexp
  regex.Comp("re").RE


  // another helpful function
  // this method makes it easier to return results to a regex function
  regex.JoinBytes("string", []byte("byte array"), 10, 'c', b(2))

  // the above method can be used in place of this one
  append(append(append(append([]byte("string"), []byte("byte array")...), []byte(strconv.Itoa(10))...), 'c'), b(2)...)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Escape

func Escape(re string) string

Escape will escape regex special chars

func IsValid

func IsValid(re string) bool

IsValid will return true if a regex is valid and can be compiled by this module

func IsValidRE2

func IsValidRE2(re string) bool

IsValidRE2 will return true if a regex is valid and can be compiled by the builtin RE2 module

func JoinBytes

func JoinBytes(bytes ...any) []byte

JoinBytes is an easy way to join multiple values into a single []byte

func TestCache

func TestCache(t *testing.T)

func TestCompile

func TestCompile(t *testing.T)

func TestConcurrent

func TestConcurrent(t *testing.T)

func TestFlags

func TestFlags(t *testing.T)

func TestPerformance

func TestPerformance(t *testing.T)

func TestReplaceFunc

func TestReplaceFunc(t *testing.T)

func TestReplaceStr

func TestReplaceStr(t *testing.T)

func TestReplaceStrComplex

func TestReplaceStrComplex(t *testing.T)

func TestValid

func TestValid(t *testing.T)

Types

type Regexp

type Regexp struct {
	RE *regexp.Regexp
	// contains filtered or unexported fields
}

func Comp

func Comp(re string, params ...string) *Regexp

Comp compiles a regular expression and store it in the cache

func CompTry

func CompTry(re string, params ...string) (*Regexp, error)

CompTry tries to compile or returns an error

func (*Regexp) Match

func (reg *Regexp) Match(buf []byte) bool

Match returns true if a []byte matches a regex

func (*Regexp) MatchFile

func (reg *Regexp) MatchFile(file *os.File, maxReSize ...int64) bool

MatchFile returns true if a file contains a regex match

func (*Regexp) Rep

func (reg *Regexp) Rep(buf []byte, rep []byte) []byte

Rep replaces a string with another string

this function will replace things in the result like $1 with your capture groups

use $0 to use the full regex capture group

use ${123} to use numbers with more than one digit

func (*Regexp) RepFile

func (reg *Regexp) RepFile(file *os.File, rep []byte, all bool, maxReSize ...int64) error

RepFile replaces a regex match with a new []byte in a file

@all: if true, will replace all text matching @re, if false, will only replace the first occurrence

func (*Regexp) RepFileFunc

func (reg *Regexp) RepFileFunc(file *os.File, rep func(data func(int) []byte) []byte, all bool, maxReSize ...int64) error

RepFileFunc replaces a regex match with the result of a callback function in a file

@all: if true, will replace all text matching @re, if false, will only replace the first occurrence

func (*Regexp) RepFunc

func (reg *Regexp) RepFunc(buf []byte, rep func(b func(int) []byte) []byte) []byte

RepFunc replaces a string with the result of a function

similar to JavaScript .replace(/re/, function(b){})

func (*Regexp) RepLit

func (reg *Regexp) RepLit(str []byte, rep []byte) []byte

RepLit replaces a string with another string literal

note: this function does not accept replacements like $1

func (*Regexp) Split

func (reg *Regexp) Split(buf []byte) [][]byte

Split splits a string, and keeps capture groups

Similar to JavaScript .split(/re/)

func (*Regexp) SplitStr

func (reg *Regexp) SplitStr(str string) []string

SplitStr splits a string, and keeps capture groups

Similar to JavaScript .split(/re/)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL