Documentation
¶
Overview ¶
包gregex提供了正则表达式功能的高性能API。 md5:5d43833868579329
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`中使用自定义替换函数`replaceFunc`替换所有匹配的`pattern`。`replaceFunc`的参数`match`类型为`[][]byte`,它包含了`pattern`使用Match函数的所有子模式的结果。 md5:cdbed5cefac02741
Example ¶
package main
import (
"bytes"
"github.com/888go/goframe/frame/g"
gregex "github.com/888go/goframe/text/gregex"
)
func main() {
var (
patternStr = `(\d+)~(\d+)`
str = "hello gf 2018~2020!"
)
// 与[ExampleReplaceFunc]不同的是,
// 结果包含了使用匹配函数的所有子模式的`pattern`。
// md5:1b711898b19df13d
result, err := gregex.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` 中所有与 `pattern` 匹配的部分 使用自定义替换函数 `replaceFunc` 进行替换。 替换函数 `replaceFunc` 的参数 `match` 类型为 []string, 它包含使用 MatchString 函数得到的 `pattern` 中所有子模式匹配结果。 md5:b24f208b16cfd56a
Example ¶
package main
import (
"github.com/888go/goframe/frame/g"
gregex "github.com/888go/goframe/text/gregex"
)
func main() {
var (
patternStr = `([A-Z])\w+`
str = "hello Golang 2018~2021!"
)
// 与[ExampleReplaceFunc]不同的是,
// 结果包含了使用匹配函数的所有子模式的`pattern`。
// md5:1b711898b19df13d
result, err := gregex.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替换字节集_函数 ¶
X替换字节集_函数 使用自定义的替换函数 `replaceFunc`,将字节切片 `src` 中所有匹配的 `pattern` 替换。 md5:3b66619bd59d4056
func X替换文本_函数 ¶
X替换文本_函数 函数会在字符串 `src` 中替换所有匹配的 `pattern`,使用自定义的替换函数 `replaceFunc`。 md5:8575760795474682
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.