正则类

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package gregex 提供了用于正则表达式功能的高性能 API。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReplaceFuncMatch

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

ReplaceFuncMatch 在字节切片 `src` 中替换所有匹配的 `pattern`, 使用自定义替换函数 `replaceFunc` 进行替换。 `replaceFunc` 参数中的 `match` 类型为 [][]byte, 它是使用 Match 函数得到的所有 `pattern` 子模式的结果。

Example
var (
	patternStr = `(\d+)~(\d+)`
	str        = "hello gf 2018~2020!"
)
// 与 [ExampleReplaceFunc] 相反
// 结果包含所有使用匹配函数的子模式的 `pattern`
result, err := 正则类.ReplaceFuncMatch(patternStr, []byte(str), func(match [][]byte) []byte {
	g.X调试输出(match)
	match[2] = []byte("2021")
	return bytes.Join(match[1:], []byte("-"))
})
g.X调试输出(result)
g.X调试输出(err)
Output:

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

func ReplaceStringFuncMatch

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

ReplaceStringFuncMatch 在字符串 `src` 中使用自定义替换函数 `replaceFunc` 替换所有匹配到的 `pattern`。 参数 `replaceFunc` 的形参类型为 `[]string`,该结果包含通过 MatchString 函数得到的所有 `pattern` 的子模式。

Example
var (
	patternStr = `([A-Z])\w+`
	str        = "hello Golang 2018~2021!"
)
// 与 [ExampleReplaceFunc] 相反
// 结果包含所有使用匹配函数的子模式的 `pattern`
result, err := 正则类.ReplaceStringFuncMatch(patternStr, str, func(match []string) string {
	g.X调试输出(match)
	match[0] = "Gf"
	return match[0]
})
g.X调试输出(result)
g.X调试输出(err)
Output:

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

func X分割

func X分割(表达式 string, 文本 string) []string

Split 函数将 `src` 切片按照表达式进行分割,并返回由这些表达式匹配之间的子字符串构成的切片。

func X匹配全部字节集

func X匹配全部字节集(表达式 string, 字节集 []byte) ([][][]byte, error)

MatchAll 返回所有匹配 `pattern` 的字节切片。

func X匹配全部文本

func X匹配全部文本(表达式 string, 文本 string) ([][]string, error)

MatchAllString 返回所有匹配 `pattern` 的字符串。

func X匹配字节集

func X匹配字节集(表达式 string, 字节集 []byte) ([][]byte, error)

Match 返回匹配 `pattern` 的字节切片。

func X匹配文本

func X匹配文本(表达式 string, 文本 string) ([]string, error)

MatchString 返回匹配`pattern`的字符串。

func X是否匹配字节集

func X是否匹配字节集(表达式 string, 字节集 []byte) bool

IsMatch 检查给定的字节序列 `src` 是否匹配模式 `pattern`。

func X是否匹配文本

func X是否匹配文本(表达式 string, 字节集 string) bool

IsMatchString 检查给定的字符串 `src` 是否与 `pattern` 匹配。

func X替换字节集

func X替换字节集(表达式 string, 替换字节集, 字节集 []byte) ([]byte, error)

Replace 将字节 `src` 中所有匹配到的 `pattern` 替换为字节 `replace`。

func X替换字节集_函数

func X替换字节集_函数(表达式 string, 字节集 []byte, 回调函数 func(b []byte) []byte) ([]byte, error)

ReplaceFunc 将字节切片 `src` 中所有匹配到的 `pattern` 用自定义替换函数 `replaceFunc` 进行替换。

func X替换文本

func X替换文本(表达式, 替换文本, 文本 string) (string, error)

ReplaceString 将字符串 `src` 中所有匹配到的 `pattern` 替换为字符串 `replace`。

func X替换文本_函数

func X替换文本_函数(表达式 string, 文本 string, 回调函数 func(s string) string) (string, error)

ReplaceStringFunc 在字符串 `src` 中使用自定义替换函数 `replaceFunc` 替换所有匹配到的 `pattern`。

func X表达式验证

func X表达式验证(表达式 string) error

Validate 检查给定的正则表达式模式 `pattern` 是否有效。

func X转义特殊符号

func X转义特殊符号(文本 string) string

Quote通过将`s`中的特殊字符替换为符合正则表达式模式的规则, 并返回处理后的副本。

例如:Quote(`[foo]`) 将返回 `\[foo\]`。

Types

This section is empty.

Jump to

Keyboard shortcuts

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