-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexpx.go
55 lines (49 loc) · 1.75 KB
/
regexpx.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
52
53
54
55
package regexpx
import (
"regexp"
)
type RegexpSet []*regexp.Regexp
// Match returns true if any expression matches s, along with the corresponding index.
// When no matches are found, an index of -1 is returned.
func (rs *RegexpSet) Match(s string) (bool, int) {
for idx, r := range []*regexp.Regexp(*rs) {
if r.FindStringIndex(s) != nil {
return true, idx
}
}
return false, -1
}
// Split slices s into all substrings separated by the first matching expression in the set.
// It returns a slice of the substrings between the matches, along with the corresponding index.
// See also regexp.Split.
func (rs *RegexpSet) Split(s string, n int) ([]string, int) {
for idx, r := range []*regexp.Regexp(*rs) {
if r.FindStringIndex(s) != nil {
return r.Split(s, n), idx
}
}
return nil, -1
}
// Replace returns a copy of s, replacing matches with the second argument, repl.
// The index of the matching expression is also returned, or -1 when no expression matches.
// See also regexp.ReplaceAllString
func (rs *RegexpSet) Replace(s, repl string) (string, int) {
for idx, r := range []*regexp.Regexp(*rs) {
if r.FindStringIndex(s) != nil {
return r.ReplaceAllString(s, repl), idx
}
}
return s, -1
}
// ReplaceSubmatch returns a copy of s, replacing matches with the second argument, repl.
// The index of the matching expression is also returned, or -1 when no expression matches.
// Any submatches (aka capturing groups) are also returned
// See also the Submatch section of the regexp docs and regexp.FindStringSubmatch
func (rs *RegexpSet) ReplaceSubmatch(s, repl string) (string, int, []string) {
for idx, r := range []*regexp.Regexp(*rs) {
if m := r.FindStringSubmatch(s); m != nil {
return r.ReplaceAllString(s, repl), idx, m
}
}
return s, -1, nil
}