-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregex.go
37 lines (31 loc) · 1010 Bytes
/
regex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/12/11
Description :
-------------------------------------------------
*/
package zutils
import (
"regexp"
"strings"
)
var Regex = ®exUtil{
escape: regexp.MustCompile(`([\^$.\[\]*\\?+{}|()])`),
escapeSimple: regexp.MustCompile(`([\^$.\[\]\\+{}|()])`),
}
type regexUtil struct {
escape *regexp.Regexp // 关闭所有regex转义字符
escapeSimple *regexp.Regexp // 不管星号和问号
}
// 关闭文本的所有正则表达式转义字符
func (u *regexUtil) MakeRegexEscapeString(text string) string {
return u.escape.ReplaceAllString(text, `\$1`)
}
// 将正则表达式转为为基础匹配语法, ?表示0或1个值, *表示任何数量的值
func (u *regexUtil) MakeRegexSimpleEscapeString(text string) string {
text = u.escapeSimple.ReplaceAllString(text, `\$1`)
text = strings.ReplaceAll(text, "?", ".?")
text = strings.ReplaceAll(text, "*", ".*?")
return text
}