2017-11-23 10:21:28 +08:00
|
|
|
package gregx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 正则表达式是否匹配
|
2017-12-28 15:21:25 +08:00
|
|
|
func IsMatch(pattern string, src []byte) bool {
|
|
|
|
match, err := regexp.Match(pattern, src)
|
2017-11-23 10:21:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return match
|
2017-12-28 15:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsMatchString(pattern string, src string) bool {
|
|
|
|
return IsMatch(pattern, []byte(src))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 正则替换(全部替换)
|
|
|
|
func Replace(pattern string, src, replace []byte) ([]byte, error) {
|
|
|
|
reg, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return src, err
|
|
|
|
}
|
|
|
|
return reg.ReplaceAll(src, replace), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 正则替换(全部替换),字符串
|
|
|
|
func ReplaceString(pattern, src, replace string) (string, error) {
|
|
|
|
r, e := Replace(pattern, []byte(src), []byte(replace))
|
|
|
|
return string(r), e
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|