regex

package module
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 13 Imported by: 5

README

Go Regex

donation link

A High Performance PCRE Regex Package That Uses A Cache.

Simplifies the the go-pcre regex package. After calling a regex, the compiled output gets cached to improve performance.

This package uses the go-pcre package for better performance.

Installation


  sudo apt-get install libpcre3-dev

  go get github.com/AspieSoft/go-regex/v4

Usage


import (
  "github.com/AspieSoft/go-regex/v4"
)

// pre compile a regex into the cache
// this method also returns the compiled pcre.Regexp struct
regex.Compile(`re`)

// compile a regex and safely escape user input
regex.Compile(`re %1`, `this will be escaped .*`); // output: this will be escaped \.\*
regex.Compile(`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.Compile(`re %1 and %2 ... %{12}`, `param 1`, `param 2` ..., `param 12`);

// manually escape a string
regex.Escape(`(.*)? \$ \\$ \\\$ regex hack failed`)

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

  return []byte("")

  // if the last option is true, returning nil will stop the loop early
  return nil
}, true /* optional: if true, will not process a return output */)

// run a simple light replace function
regex.Compile(`re`).RepStr(myByteArray, myReplacementByteArray)

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

// split a byte array in a similar way to JavaScript
regex.Compile(`re|(keep this and split like in JavaScript)`).Split(myByteArray)

// 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)`

// an alias of pcre.Regexp
regex.PCRE

// direct access to compiled pcre.Regexp
regex.Compile("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', data(2))

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

Optional

For a more verbose version of this module, with longer function names.


  import (
    "github.com/AspieSoft/go-regex/v4/verbose"
  )

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoInstallLinuxDependencies

func AutoInstallLinuxDependencies()

AutoInstallLinuxDependencies will automatically detect and install dependencies if missing from debian or arch linux debian: libpcre3-dev arch: pcre-dev

func Escape

func Escape(re string) string

Escape will escape regex special chars

func IsValid added in v4.2.0

func IsValid(str []byte) bool

func IsValidRef added in v4.2.0

func IsValidRef(str *[]byte) bool

func JoinBytes

func JoinBytes(bytes ...interface{}) []byte

JoinBytes is an easy way to join multiple values into a single []byte accepts: []byte, byte, int32, string, [][]byte, int, int64, float64, float32

Types

type PCRE

type PCRE = pcre.Regexp

type Regexp

type Regexp struct {
	RE *pcre.Regexp
}

func Comp added in v4.1.0

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

An alias for Compile

func Compile

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

Compile compiles a regular expression and store it in the cache

func (*Regexp) Match

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

Match returns true if a []byte matches a regex

func (*Regexp) MatchRef

func (reg *Regexp) MatchRef(str *[]byte) bool

MatchRef returns true if a string matches a regex Uses Pointers For Improved Performance

func (*Regexp) RepFunc

func (reg *Regexp) RepFunc(str []byte, rep func(data func(int) []byte) []byte, blank ...bool) []byte

RepFunc replaces a string with the result of a function similar to JavaScript .replace(/re/, function(data){})

func (*Regexp) RepFuncFirst

func (reg *Regexp) RepFuncFirst(str []byte, rep func(func(int) []byte) []byte, blank ...bool) []byte

RepFuncFirst is a copy of the RepFunc method modified to only run once

func (*Regexp) RepFuncRef

func (reg *Regexp) RepFuncRef(str *[]byte, rep func(data func(int) []byte) []byte, blank ...bool) []byte

RepFuncRef replace a string with the result of a function similar to JavaScript .replace(/re/, function(data){}) Uses Pointers For Improved Performance

func (*Regexp) RepStr

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

RepStr replaces a string with another string note: this function is optimized for performance, and the replacement string does not accept replacements like $1

func (*Regexp) RepStrComp added in v4.1.0

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

An alias for RepStrComplex

func (*Regexp) RepStrCompRef added in v4.1.0

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

An alias for RepStrComplexRef

func (*Regexp) RepStrCompRefRes added in v4.1.0

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

An alias for RepStrComplexRefRes

func (*Regexp) RepStrComplex

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

RepStrComplex is a more complex version of the RepStr method 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) RepStrComplexRef

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

RepStrComplexRef is a more complex version of the RepStrRef method 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 Uses Pointers For Improved Performance

func (*Regexp) RepStrComplexRefRes

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

RepStrComplexRefRes is a more complex version of the RepStrRefRes method 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 Uses Pointers For Improved Performance (also on result)

func (*Regexp) RepStrRef

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

RepStrRef replaces a string with another string note: this function is optimized for performance, and the replacement string does not accept replacements like $1 Uses Pointers For Improved Performance

func (*Regexp) RepStrRefRes

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

RepStrRefRes replaces a string with another string note: this function is optimized for performance, and the replacement string does not accept replacements like $1 Uses Pointers For Improved Performance (also on result)

func (*Regexp) Split

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

Split splits a string, and keeps capture groups Similar to JavaScript .split(/re/)

func (*Regexp) SplitRef

func (reg *Regexp) SplitRef(str *[]byte) [][]byte

SplitRef splits a string, and keeps capture groups Similar to JavaScript .split(/re/) Uses Pointers For Improved Performance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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