1 package sprig
2
3 import (
4 "regexp"
5 )
6
7 func regexMatch(regex string, s string) bool {
8 match, _ := regexp.MatchString(regex, s)
9 return match
10 }
11
12 func mustRegexMatch(regex string, s string) (bool, error) {
13 return regexp.MatchString(regex, s)
14 }
15
16 func regexFindAll(regex string, s string, n int) []string {
17 r := regexp.MustCompile(regex)
18 return r.FindAllString(s, n)
19 }
20
21 func mustRegexFindAll(regex string, s string, n int) ([]string, error) {
22 r, err := regexp.Compile(regex)
23 if err != nil {
24 return []string{}, err
25 }
26 return r.FindAllString(s, n), nil
27 }
28
29 func regexFind(regex string, s string) string {
30 r := regexp.MustCompile(regex)
31 return r.FindString(s)
32 }
33
34 func mustRegexFind(regex string, s string) (string, error) {
35 r, err := regexp.Compile(regex)
36 if err != nil {
37 return "", err
38 }
39 return r.FindString(s), nil
40 }
41
42 func regexReplaceAll(regex string, s string, repl string) string {
43 r := regexp.MustCompile(regex)
44 return r.ReplaceAllString(s, repl)
45 }
46
47 func mustRegexReplaceAll(regex string, s string, repl string) (string, error) {
48 r, err := regexp.Compile(regex)
49 if err != nil {
50 return "", err
51 }
52 return r.ReplaceAllString(s, repl), nil
53 }
54
55 func regexReplaceAllLiteral(regex string, s string, repl string) string {
56 r := regexp.MustCompile(regex)
57 return r.ReplaceAllLiteralString(s, repl)
58 }
59
60 func mustRegexReplaceAllLiteral(regex string, s string, repl string) (string, error) {
61 r, err := regexp.Compile(regex)
62 if err != nil {
63 return "", err
64 }
65 return r.ReplaceAllLiteralString(s, repl), nil
66 }
67
68 func regexSplit(regex string, s string, n int) []string {
69 r := regexp.MustCompile(regex)
70 return r.Split(s, n)
71 }
72
73 func mustRegexSplit(regex string, s string, n int) ([]string, error) {
74 r, err := regexp.Compile(regex)
75 if err != nil {
76 return []string{}, err
77 }
78 return r.Split(s, n), nil
79 }
80
81 func regexQuoteMeta(s string) string {
82 return regexp.QuoteMeta(s)
83 }
84
View as plain text