gf/text/gregex/gregex.go

150 lines
4.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2017-12-29 16:03:30 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2017-12-29 16:03:30 +08:00
2019-01-16 09:00:23 +08:00
// Package gregex provides high performance API for regular expression functionality.
2018-07-11 17:06:47 +08:00
package gregex
2017-11-23 10:21:28 +08:00
import (
2019-06-19 09:06:52 +08:00
"regexp"
2017-11-23 10:21:28 +08:00
)
// Quote quotes `s` by replacing special chars in `s`
2019-02-19 11:19:23 +08:00
// to match the rules of regular expression pattern.
// And returns the copy.
//
// Eg: Quote(`[foo]`) returns `\[foo\]`.
func Quote(s string) string {
2019-06-19 09:06:52 +08:00
return regexp.QuoteMeta(s)
}
// Validate checks whether given regular expression pattern `pattern` valid.
2018-04-11 12:05:25 +08:00
func Validate(pattern string) error {
2019-06-19 09:06:52 +08:00
_, err := getRegexp(pattern)
return err
2018-04-11 12:05:25 +08:00
}
// IsMatch checks whether given bytes `src` matches `pattern`.
2017-12-28 15:21:25 +08:00
func IsMatch(pattern string, src []byte) bool {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.Match(src)
}
return false
2017-12-28 15:21:25 +08:00
}
// IsMatchString checks whether given string `src` matches `pattern`.
2017-12-28 15:21:25 +08:00
func IsMatchString(pattern string, src string) bool {
2019-06-19 09:06:52 +08:00
return IsMatch(pattern, []byte(src))
2017-12-28 15:21:25 +08:00
}
2021-11-01 19:46:39 +08:00
// Match return bytes slice that matched `pattern`.
2019-01-28 12:03:47 +08:00
func Match(pattern string, src []byte) ([][]byte, error) {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.FindSubmatch(src), nil
} else {
return nil, err
}
2019-01-28 12:03:47 +08:00
}
// MatchString return strings that matched `pattern`.
2017-12-29 15:42:42 +08:00
func MatchString(pattern string, src string) ([]string, error) {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.FindStringSubmatch(src), nil
} else {
return nil, err
}
2017-12-29 15:42:42 +08:00
}
// MatchAll return all bytes slices that matched `pattern`.
2019-01-28 12:03:47 +08:00
func MatchAll(pattern string, src []byte) ([][][]byte, error) {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.FindAllSubmatch(src, -1), nil
} else {
return nil, err
}
2019-01-28 12:03:47 +08:00
}
// MatchAllString return all strings that matched `pattern`.
2017-12-29 15:42:42 +08:00
func MatchAllString(pattern string, src string) ([][]string, error) {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.FindAllStringSubmatch(src, -1), nil
} else {
return nil, err
}
2017-12-29 15:42:42 +08:00
}
2021-11-16 23:39:45 +08:00
// Replace replaces all matched `pattern` in bytes `src` with bytes `replace`.
2018-03-08 09:37:19 +08:00
func Replace(pattern string, replace, src []byte) ([]byte, error) {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.ReplaceAll(src, replace), nil
} else {
return nil, err
}
2017-12-28 15:21:25 +08:00
}
// ReplaceString replace all matched `pattern` in string `src` with string `replace`.
2018-03-08 09:37:19 +08:00
func ReplaceString(pattern, replace, src string) (string, error) {
2019-06-19 09:06:52 +08:00
r, e := Replace(pattern, []byte(replace), []byte(src))
return string(r), e
}
// ReplaceFunc replace all matched `pattern` in bytes `src`
// with custom replacement function `replaceFunc`.
2019-01-28 12:03:47 +08:00
func ReplaceFunc(pattern string, src []byte, replaceFunc func(b []byte) []byte) ([]byte, error) {
if r, err := getRegexp(pattern); err == nil {
return r.ReplaceAllFunc(src, replaceFunc), nil
} else {
return nil, err
}
}
// 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.
func ReplaceFuncMatch(pattern string, src []byte, replaceFunc func(match [][]byte) []byte) ([]byte, error) {
if r, err := getRegexp(pattern); err == nil {
return r.ReplaceAllFunc(src, func(bytes []byte) []byte {
2019-08-31 18:04:12 +08:00
match, _ := Match(pattern, bytes)
return replaceFunc(match)
}), nil
} else {
return nil, err
}
}
// ReplaceStringFunc replace all matched `pattern` in string `src`
// with custom replacement function `replaceFunc`.
2019-01-28 12:03:47 +08:00
func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) string) (string, error) {
2019-06-19 09:06:52 +08:00
bytes, err := ReplaceFunc(pattern, []byte(src), func(bytes []byte) []byte {
return []byte(replaceFunc(string(bytes)))
})
return string(bytes), err
2019-02-14 13:38:52 +08:00
}
// 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.
func ReplaceStringFuncMatch(pattern string, src string, replaceFunc func(match []string) string) (string, error) {
if r, err := getRegexp(pattern); err == nil {
return string(r.ReplaceAllFunc([]byte(src), func(bytes []byte) []byte {
2019-08-31 18:04:12 +08:00
match, _ := MatchString(pattern, string(bytes))
return []byte(replaceFunc(match))
})), nil
} else {
return "", err
}
}
// Split slices `src` into substrings separated by the expression and returns a slice of
2019-02-14 13:38:52 +08:00
// the substrings between those expression matches.
func Split(pattern string, src string) []string {
2019-06-19 09:06:52 +08:00
if r, err := getRegexp(pattern); err == nil {
return r.Split(src, -1)
}
return nil
2019-02-14 13:38:52 +08:00
}