-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexpfilter.go
51 lines (43 loc) · 1.09 KB
/
regexpfilter.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
41
42
43
44
45
46
47
48
49
50
51
package nettrigger
import (
"errors"
"fmt"
"regexp"
"strings"
)
type regexpFilter struct {
subject string
re *regexp.Regexp
}
func (f regexpFilter) Filter(env Environment) bool {
return f.re.MatchString(env.Expand(f.subject))
}
// RegexpBuilder constructs regular expression filters from filter
// specifications.
func RegexpBuilder(spec FilterSpec) (Filter, error) {
switch strings.ToLower(spec.Type) {
case "regexp", "regex", "re":
default:
return nil, nil
}
switch len(spec.Args) {
case 0, 1:
return nil, errors.New("regular expression filter requires a subject and an expression")
case 2:
subject, expression := spec.Args[0], spec.Args[1]
// Force case-insensitive matching
if !strings.HasPrefix(expression, "(?i)") {
expression = "(?i)" + expression
}
re, err := regexp.Compile(expression)
if err != nil {
return nil, fmt.Errorf("invalid expression \"%s\": %v", expression, err)
}
return regexpFilter{
subject: subject,
re: re,
}.Filter, nil
default:
return nil, errors.New("regular expression filter has %d arguments when two are needed")
}
}