gregex

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 3 Imported by: 148

Documentation

Overview

Package gregex provides high performance API for regular expression functionality.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsMatch

func IsMatch(pattern string, src []byte) bool

IsMatch checks whether given bytes `src` matches `pattern`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `\d+`
	g.Dump(gregex.IsMatch(patternStr, []byte("hello 2022! hello gf!")))
	g.Dump(gregex.IsMatch(patternStr, nil))
	g.Dump(gregex.IsMatch(patternStr, []byte("hello gf!")))

}
Output:

true
false
false

func IsMatchString

func IsMatchString(pattern string, src string) bool

IsMatchString checks whether given string `src` matches `pattern`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `\d+`
	g.Dump(gregex.IsMatchString(patternStr, "hello 2022! hello gf!"))
	g.Dump(gregex.IsMatchString(patternStr, "hello gf!"))
	g.Dump(gregex.IsMatchString(patternStr, ""))

}
Output:

true
false
false

func Match

func Match(pattern string, src []byte) ([][]byte, error)

Match return bytes slice that matched `pattern`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `(\w+)=(\w+)`
	matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
	// This method looks for the first match index
	result, err := gregex.Match(patternStr, []byte(matchStr))
	g.Dump(result)
	g.Dump(err)

}
Output:

[
    "pageId=1114219",
    "pageId",
    "1114219",
]
<nil>

func MatchAll

func MatchAll(pattern string, src []byte) ([][][]byte, error)

MatchAll return all bytes slices that matched `pattern`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `(\w+)=(\w+)`
	matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
	result, err := gregex.MatchAll(patternStr, []byte(matchStr))
	g.Dump(result)
	g.Dump(err)

}
Output:

 [
    [
        "pageId=1114219",
        "pageId",
        "1114219",
    ],
    [
        "searchId=8QC5D1D2E",
        "searchId",
        "8QC5D1D2E",
    ],
]
<nil>

func MatchAllString

func MatchAllString(pattern string, src string) ([][]string, error)

MatchAllString return all strings that matched `pattern`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `(\w+)=(\w+)`
	matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
	result, err := gregex.MatchAllString(patternStr, matchStr)
	g.Dump(result)
	g.Dump(err)

}
Output:

[
    [
        "pageId=1114219",
        "pageId",
        "1114219",
    ],
    [
        "searchId=8QC5D1D2E",
        "searchId",
        "8QC5D1D2E",
    ],
]
<nil>

func MatchString

func MatchString(pattern string, src string) ([]string, error)

MatchString return strings that matched `pattern`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `(\w+)=(\w+)`
	matchStr := "https://goframe.org/pages/viewpage.action?pageId=1114219&searchId=8QC5D1D2E!"
	// This method looks for the first match index
	result, err := gregex.MatchString(patternStr, matchStr)
	g.Dump(result)
	g.Dump(err)

}
Output:

[
    "pageId=1114219",
    "pageId",
    "1114219",
]
<nil>

func Quote

func Quote(s string) string

Quote quotes `s` by replacing special chars in `s` to match the rules of regular expression pattern. And returns the copy.

Eg: Quote(`[foo]`) returns `\[foo\]`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	result := gregex.Quote(`[1-9]\d+`)
	fmt.Println(result)

}
Output:

\[1-9\]\\d\+

func Replace

func Replace(pattern string, replace, src []byte) ([]byte, error)

Replace replaces all matched `pattern` in bytes `src` with bytes `replace`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	var (
		patternStr  = `\d+`
		str         = "hello gf 2020!"
		repStr      = "2021"
		result, err = gregex.Replace(patternStr, []byte(repStr), []byte(str))
	)
	g.Dump(err)
	g.Dump(result)

}
Output:

<nil>
"hello gf 2021!"

func ReplaceFunc

func ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error)

ReplaceFunc replace all matched `pattern` in bytes `src` with custom replacement function `replaceFunc`.

Example
package main

import (
	"bytes"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	// In contrast to [ExampleReplaceFunc]
	// the result contains the `pattern' of all subpattern that use the matching function
	result, err := gregex.ReplaceFuncMatch(`(\d+)~(\d+)`, []byte("hello gf 2018~2020!"), func(match [][]byte) []byte {
		g.Dump(match)
		match[2] = []byte("2021")
		return bytes.Join(match[1:], []byte("~"))
	})
	g.Dump(result)
	g.Dump(err)

}
Output:

[
    "2018~2020",
    "2018",
    "2020",
]
"hello gf 2018~2021!"
<nil>

func ReplaceFuncMatch

func ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)

ReplaceFuncMatch replace all matched `pattern` in bytes `src` with custom replacement function `replaceFunc`. The parameter `match` type for `replaceFunc` is [][]byte, which is the result contains all sub-patterns of `pattern` using Match function.

Example
package main

import (
	"bytes"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	var (
		patternStr = `(\d+)~(\d+)`
		str        = "hello gf 2018~2020!"
	)
	// In contrast to [ExampleReplaceFunc]
	// the result contains the `pattern' of all subpatterns that use the matching function
	result, err := gregex.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte {
		g.Dump(match)
		match[2] = []byte("2021")
		return bytes.Join(match[1:], []byte("-"))
	})
	g.Dump(result)
	g.Dump(err)

}
Output:

[
    "2018~2020",
    "2018",
    "2020",
]
"hello gf 2018-2021!"
<nil>

func ReplaceString

func ReplaceString(pattern, replace, src string) (string, error)

ReplaceString replace all matched `pattern` in string `src` with string `replace`.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `\d+`
	str := "hello gf 2020!"
	replaceStr := "2021"
	result, err := gregex.ReplaceString(patternStr, replaceStr, str)

	g.Dump(result)
	g.Dump(err)

}
Output:

"hello gf 2021!"
<nil>

func ReplaceStringFunc

func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error)

ReplaceStringFunc replace all matched `pattern` in string `src` with custom replacement function `replaceFunc`.

Example
package main

import (
	"strings"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	replaceStrMap := map[string]string{
		"2020": "2021",
	}
	// When the regular statement can match multiple results
	// func can be used to further control the value that needs to be modified
	result, err := gregex.ReplaceStringFunc(`\d+`, `hello gf 2018~2020!`, func(b string) string {
		g.Dump(b)
		if replaceStr, ok := replaceStrMap[b]; ok {
			return replaceStr
		}
		return b
	})
	g.Dump(result)
	g.Dump(err)

	result, err = gregex.ReplaceStringFunc(`[a-z]*`, "gf@goframe.org", strings.ToUpper)
	g.Dump(result)
	g.Dump(err)

}
Output:

"2018"
"2020"
"hello gf 2018~2021!"
<nil>
"GF@GOFRAME.ORG"
<nil>

func ReplaceStringFuncMatch

func ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error)

ReplaceStringFuncMatch replace all matched `pattern` in string `src` with custom replacement function `replaceFunc`. The parameter `match` type for `replaceFunc` is []string, which is the result contains all sub-patterns of `pattern` using MatchString function.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	var (
		patternStr = `([A-Z])\w+`
		str        = "hello Golang 2018~2021!"
	)
	// In contrast to [ExampleReplaceFunc]
	// the result contains the `pattern' of all subpatterns that use the matching function
	result, err := gregex.ReplaceStringFuncMatch(patternStr, str, func(match []string) string {
		g.Dump(match)
		match[0] = "Gf"
		return match[0]
	})
	g.Dump(result)
	g.Dump(err)

}
Output:

[
    "Golang",
    "G",
]
"hello Gf 2018~2021!"
<nil>

func Split

func Split(pattern string, src string) []string

Split slices `src` into substrings separated by the expression and returns a slice of the substrings between those expression matches.

Example
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	patternStr := `\d+`
	str := "hello2020gf"
	result := gregex.Split(patternStr, str)
	g.Dump(result)

}
Output:

[
    "hello",
    "gf",
]

func Validate

func Validate(pattern string) error

Validate checks whether given regular expression pattern `pattern` valid.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/text/gregex"
)

func main() {
	// Valid match statement
	fmt.Println(gregex.Validate(`\d+`))
	// Mismatched statement
	fmt.Println(gregex.Validate(`[a-9]\d+`))

}
Output:

<nil>
regexp.Compile failed for pattern "[a-9]\d+": error parsing regexp: invalid character class range: `a-9`

Types

This section is empty.

Jump to

Keyboard shortcuts

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