-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregexp.go
60 lines (46 loc) · 923 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package stdlib
import (
"regexp"
)
func _regexp(re, s string) bool {
c, err := regexp.Compile(re)
if err != nil {
return false
}
return c.MatchString(s)
}
func regexpSplitPart(_s, _split, _part any) string {
s := stringy(_s)
split := stringy(_split)
part := int(floaty(_part))
c, err := regexp.Compile(split)
if err != nil {
return ""
}
pieces := part + 1
if pieces == 0 {
pieces = -1
}
parts := c.Split(s, pieces)
if len(parts) == 0 || part >= len(parts) {
return ""
}
if part < 0 {
part = (len(parts) + part) % len(parts)
}
return parts[part]
}
func regexpCount(_s, _re any) int64 {
s := stringy(_s)
re := stringy(_re)
c, err := regexp.Compile(re)
if err != nil {
return 0
}
return int64(len(c.FindAllStringIndex(s, -1)))
}
var regexpFunctions = map[string]any{
"regexp": _regexp,
"regexp_split_part": regexpSplitPart,
"regexp_count": regexpCount,
}