-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.go
40 lines (32 loc) · 815 Bytes
/
regexp.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
38
39
40
// This is not a public method package, it is recommended to use an external wrapper.
package regexp
import (
"errors"
"github.com/grafana/regexp"
"github.com/3JoB/ulib/litefmt"
)
var ErrNotCompiled error = errors.New("not compiled")
type KeywordCompile struct {
Compile *regexp.Regexp
}
// DO NOT USE!
//
// This is not a public function!
func (c *KeywordCompile) Init(keyword string) (err error) {
keyword = litefmt.Sprint("(^|\\s)", keyword, "(\\s|$)")
c.Compile, err = regexp.Compile(keyword)
return
}
func (c *KeywordCompile) Find(text string) bool {
if c.Compile == nil && text == "" {
return false
}
return c.Compile.MatchString(text)
}
func Match(text, keyword string) bool {
if match, err := regexp.MatchString(keyword, text); err == nil {
return match
} else {
return false
}
}