-
Notifications
You must be signed in to change notification settings - Fork 16
/
regexp.go
37 lines (33 loc) · 982 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
package stream
import "regexp"
// Grep emits every input x that matches the regular expression r.
func Grep(r string) Filter {
re, err := regexp.Compile(r)
if err != nil {
return FilterFunc(func(Arg) error { return err })
}
return If(re.MatchString)
}
// GrepNot emits every input x that does not match the regular expression r.
func GrepNot(r string) Filter {
re, err := regexp.Compile(r)
if err != nil {
return FilterFunc(func(Arg) error { return err })
}
return If(func(s string) bool { return !re.MatchString(s) })
}
// Substitute replaces all occurrences of the regular expression r in
// an input item with replacement. The replacement string can contain
// $1, $2, etc. which represent submatches of r.
func Substitute(r, replacement string) Filter {
return FilterFunc(func(arg Arg) error {
re, err := regexp.Compile(r)
if err != nil {
return err
}
for s := range arg.In {
arg.Out <- re.ReplaceAllString(s, replacement)
}
return nil
})
}