2018-05-28 13:58:59 +08:00
|
|
|
|
// Copyright 2017-2018 gf Author(https://gitee.com/johng/gf). 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://gitee.com/johng/gf.
|
|
|
|
|
|
2018-03-08 09:37:19 +08:00
|
|
|
|
// 正则表达式.
|
2018-07-11 17:06:47 +08:00
|
|
|
|
package gregex
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
2018-05-28 13:58:59 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gcache"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-05-28 13:58:59 +08:00
|
|
|
|
// 缓存对象,主要用于缓存底层regx对象
|
|
|
|
|
var regxCache = gcache.New()
|
|
|
|
|
|
|
|
|
|
// 根据pattern生成对应的regexp正则对象
|
|
|
|
|
func getRegexp(pattern string) (*regexp.Regexp, error) {
|
|
|
|
|
if v := regxCache.Get(pattern); v != nil {
|
|
|
|
|
return v.(*regexp.Regexp), nil
|
|
|
|
|
}
|
|
|
|
|
if r, err := regexp.Compile(pattern); err == nil {
|
|
|
|
|
regxCache.Set(pattern, r, 0)
|
|
|
|
|
return r, nil
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 22:00:51 +08:00
|
|
|
|
// 转移正则规则字符串,例如:Quote(`[foo]`) 返回 `\[foo\]`
|
|
|
|
|
func Quote(s string) string {
|
|
|
|
|
return regexp.QuoteMeta(s)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 12:05:25 +08:00
|
|
|
|
// 校验所给定的正则表达式是否符合规范
|
|
|
|
|
func Validate(pattern string) error {
|
2018-05-28 13:58:59 +08:00
|
|
|
|
_, err := getRegexp(pattern)
|
2018-04-11 12:05:25 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 正则表达式是否匹配
|
2017-12-28 15:21:25 +08:00
|
|
|
|
func IsMatch(pattern string, src []byte) bool {
|
2018-05-28 13:58:59 +08:00
|
|
|
|
if r, err := getRegexp(pattern); err == nil {
|
|
|
|
|
return r.Match(src)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-05-28 13:58:59 +08:00
|
|
|
|
return false
|
2017-12-28 15:21:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsMatchString(pattern string, src string) bool {
|
|
|
|
|
return IsMatch(pattern, []byte(src))
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-29 15:42:42 +08:00
|
|
|
|
// 正则匹配,并返回匹配的列表
|
|
|
|
|
func MatchString(pattern string, src string) ([]string, error) {
|
2018-05-28 13:58:59 +08:00
|
|
|
|
if r, err := getRegexp(pattern); err == nil {
|
|
|
|
|
return r.FindStringSubmatch(src), nil
|
|
|
|
|
} else {
|
2017-12-29 15:42:42 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MatchAllString(pattern string, src string) ([][]string, error) {
|
2018-05-28 13:58:59 +08:00
|
|
|
|
if r, err := getRegexp(pattern); err == nil {
|
|
|
|
|
return r.FindAllStringSubmatch(src, -1), nil
|
|
|
|
|
} else {
|
2017-12-29 15:42:42 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 15:21:25 +08:00
|
|
|
|
// 正则替换(全部替换)
|
2018-03-08 09:37:19 +08:00
|
|
|
|
func Replace(pattern string, replace, src []byte) ([]byte, error) {
|
2018-05-28 13:58:59 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 正则替换(全部替换),字符串
|
2018-03-08 09:37:19 +08:00
|
|
|
|
func ReplaceString(pattern, replace, src string) (string, error) {
|
2018-03-13 17:57:41 +08:00
|
|
|
|
r, e := Replace(pattern, []byte(replace), []byte(src))
|
2017-12-28 15:21:25 +08:00
|
|
|
|
return string(r), e
|
2018-07-24 22:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 正则替换(全部替换),给定自定义替换方法
|
|
|
|
|
func ReplaceFunc(pattern string, src []byte, repl func(b []byte) []byte) ([]byte, error) {
|
|
|
|
|
if r, err := getRegexp(pattern); err == nil {
|
|
|
|
|
return r.ReplaceAllFunc(src, repl), nil
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 正则替换(全部替换),给定自定义替换方法
|
|
|
|
|
func ReplaceStringFunc(pattern string, src string, repl func(s string) string) (string, error) {
|
|
|
|
|
bytes, err := ReplaceFunc(pattern, []byte(src), func(bytes []byte) []byte {
|
|
|
|
|
return []byte(repl(string(bytes)))
|
|
|
|
|
})
|
|
|
|
|
return string(bytes), err
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|