forked from foomo/petze
/
regex.go
37 lines (33 loc) · 961 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
package check
import (
"github.com/foomo/petze/config"
"regexp"
"strings"
)
func Regex(data []byte, selector string, expect config.Expect) (ok bool, info string) {
regex, errCompile := regexp.Compile(selector)
if errCompile != nil {
return false, "could not compile regex '" + selector + "'"
}
res := regex.FindAll(data, -1)
switch true {
case expect.Min != nil || expect.Max != nil || expect.Count != nil:
return checkMinMaxCount(expect, int64(len(res)))
case expect.Equals != nil:
for _, res := range res {
if string(res) == expect.Equals {
return true, "regex match found"
}
}
return false, "could not find regex result equals"
case expect.Contains != "":
for _, res := range res {
if strings.Contains(string(res), expect.Contains) {
return true, "regex contains substring found"
}
}
return false, "could not find regex result contains"
default:
return false, "comparator not implemented for regex"
}
}