Documentation ¶
Overview ¶
Package gregex 提供了用于正则表达式功能的高性能 API。
Index ¶
- func ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error)
- func ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error)
- func X分割(表达式 string, 文本 string) []string
- func X匹配全部字节集(表达式 string, 字节集 []byte) ([][][]byte, error)
- func X匹配全部文本(表达式 string, 文本 string) ([][]string, error)
- func X匹配字节集(表达式 string, 字节集 []byte) ([][]byte, error)
- func X匹配文本(表达式 string, 文本 string) ([]string, error)
- func X是否匹配字节集(表达式 string, 字节集 []byte) bool
- func X是否匹配文本(表达式 string, 字节集 string) bool
- func X替换字节集(表达式 string, 替换字节集, 字节集 []byte) ([]byte, error)
- func X替换字节集_函数(表达式 string, 字节集 []byte, 回调函数 func(b []byte) []byte) ([]byte, error)
- func X替换文本(表达式, 替换文本, 文本 string) (string, error)
- func X替换文本_函数(表达式 string, 文本 string, 回调函数 func(s string) string) (string, error)
- func X表达式验证(表达式 string) error
- func X转义特殊符号(文本 string) string
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>
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.