Debian Codegraph @

util/sprig/regex.go

raw
1 package sprig
2
3 import (
4 "regexp"
5 )
6
7 // regexMatch checks if a string matches a regular expression pattern.
8 // It ignores any errors that might occur during regex compilation.
9 //
10 // Parameters:
11 // - regex: The regular expression pattern to match against
12 // - s: The string to check
13 //
14 // Returns:
15 // - bool: True if the string matches the pattern, false otherwise
16 func regexMatch(regex string, s string) bool {
17 match, _ := regexp.MatchString(regex, s)
18 return match
19 }
20
21 // mustRegexMatch checks if a string matches a regular expression pattern.
22 // Unlike regexMatch, this function returns any errors that occur during regex compilation.
23 //
24 // Parameters:
25 // - regex: The regular expression pattern to match against
26 // - s: The string to check
27 //
28 // Returns:
29 // - bool: True if the string matches the pattern, false otherwise
30 // - error: Any error that occurred during regex compilation
31 func mustRegexMatch(regex string, s string) (bool, error) {
32 return regexp.MatchString(regex, s)
33 }
34
35 // regexFindAll finds all matches of a regular expression in a string.
36 // It panics if the regex pattern cannot be compiled.
37 //
38 // Parameters:
39 // - regex: The regular expression pattern to search for
40 // - s: The string to search within
41 // - n: The maximum number of matches to return (negative means all matches)
42 //
43 // Returns:
44 // - []string: A slice containing all matched substrings
45 func regexFindAll(regex string, s string, n int) []string {
46 r := regexp.MustCompile(regex)
47 return r.FindAllString(s, n)
48 }
49
50 // mustRegexFindAll finds all matches of a regular expression in a string.
51 // Unlike regexFindAll, this function returns any errors that occur during regex compilation.
52 //
53 // Parameters:
54 // - regex: The regular expression pattern to search for
55 // - s: The string to search within
56 // - n: The maximum number of matches to return (negative means all matches)
57 //
58 // Returns:
59 // - []string: A slice containing all matched substrings
60 // - error: Any error that occurred during regex compilation
61 func mustRegexFindAll(regex string, s string, n int) ([]string, error) {
62 r, err := regexp.Compile(regex)
63 if err != nil {
64 return []string{}, err
65 }
66 return r.FindAllString(s, n), nil
67 }
68
69 // regexFind finds the first match of a regular expression in a string.
70 // It panics if the regex pattern cannot be compiled.
71 //
72 // Parameters:
73 // - regex: The regular expression pattern to search for
74 // - s: The string to search within
75 //
76 // Returns:
77 // - string: The first matched substring, or an empty string if no match
78 func regexFind(regex string, s string) string {
79 r := regexp.MustCompile(regex)
80 return r.FindString(s)
81 }
82
83 // mustRegexFind finds the first match of a regular expression in a string.
84 // Unlike regexFind, this function returns any errors that occur during regex compilation.
85 //
86 // Parameters:
87 // - regex: The regular expression pattern to search for
88 // - s: The string to search within
89 //
90 // Returns:
91 // - string: The first matched substring, or an empty string if no match
92 // - error: Any error that occurred during regex compilation
93 func mustRegexFind(regex string, s string) (string, error) {
94 r, err := regexp.Compile(regex)
95 if err != nil {
96 return "", err
97 }
98 return r.FindString(s), nil
99 }
100
101 // regexReplaceAll replaces all matches of a regular expression with a replacement string.
102 // It panics if the regex pattern cannot be compiled.
103 // The replacement string can contain $1, $2, etc. for submatches.
104 //
105 // Parameters:
106 // - regex: The regular expression pattern to search for
107 // - s: The string to search within
108 // - repl: The replacement string (can contain $1, $2, etc. for submatches)
109 //
110 // Returns:
111 // - string: The resulting string after all replacements
112 func regexReplaceAll(regex string, s string, repl string) string {
113 r := regexp.MustCompile(regex)
114 return r.ReplaceAllString(s, repl)
115 }
116
117 // mustRegexReplaceAll replaces all matches of a regular expression with a replacement string.
118 // Unlike regexReplaceAll, this function returns any errors that occur during regex compilation.
119 // The replacement string can contain $1, $2, etc. for submatches.
120 //
121 // Parameters:
122 // - regex: The regular expression pattern to search for
123 // - s: The string to search within
124 // - repl: The replacement string (can contain $1, $2, etc. for submatches)
125 //
126 // Returns:
127 // - string: The resulting string after all replacements
128 // - error: Any error that occurred during regex compilation
129 func mustRegexReplaceAll(regex string, s string, repl string) (string, error) {
130 r, err := regexp.Compile(regex)
131 if err != nil {
132 return "", err
133 }
134 return r.ReplaceAllString(s, repl), nil
135 }
136
137 // regexReplaceAllLiteral replaces all matches of a regular expression with a literal replacement string.
138 // It panics if the regex pattern cannot be compiled.
139 // Unlike regexReplaceAll, the replacement string is used literally (no $1, $2 processing).
140 //
141 // Parameters:
142 // - regex: The regular expression pattern to search for
143 // - s: The string to search within
144 // - repl: The literal replacement string
145 //
146 // Returns:
147 // - string: The resulting string after all replacements
148 func regexReplaceAllLiteral(regex string, s string, repl string) string {
149 r := regexp.MustCompile(regex)
150 return r.ReplaceAllLiteralString(s, repl)
151 }
152
153 // mustRegexReplaceAllLiteral replaces all matches of a regular expression with a literal replacement string.
154 // Unlike regexReplaceAllLiteral, this function returns any errors that occur during regex compilation.
155 // The replacement string is used literally (no $1, $2 processing).
156 //
157 // Parameters:
158 // - regex: The regular expression pattern to search for
159 // - s: The string to search within
160 // - repl: The literal replacement string
161 //
162 // Returns:
163 // - string: The resulting string after all replacements
164 // - error: Any error that occurred during regex compilation
165 func mustRegexReplaceAllLiteral(regex string, s string, repl string) (string, error) {
166 r, err := regexp.Compile(regex)
167 if err != nil {
168 return "", err
169 }
170 return r.ReplaceAllLiteralString(s, repl), nil
171 }
172
173 // regexSplit splits a string by a regular expression pattern.
174 // It panics if the regex pattern cannot be compiled.
175 //
176 // Parameters:
177 // - regex: The regular expression pattern to split on
178 // - s: The string to split
179 // - n: The maximum number of substrings to return (negative means all substrings)
180 //
181 // Returns:
182 // - []string: A slice containing the substrings between regex matches
183 func regexSplit(regex string, s string, n int) []string {
184 r := regexp.MustCompile(regex)
185 return r.Split(s, n)
186 }
187
188 // mustRegexSplit splits a string by a regular expression pattern.
189 // Unlike regexSplit, this function returns any errors that occur during regex compilation.
190 //
191 // Parameters:
192 // - regex: The regular expression pattern to split on
193 // - s: The string to split
194 // - n: The maximum number of substrings to return (negative means all substrings)
195 //
196 // Returns:
197 // - []string: A slice containing the substrings between regex matches
198 // - error: Any error that occurred during regex compilation
199 func mustRegexSplit(regex string, s string, n int) ([]string, error) {
200 r, err := regexp.Compile(regex)
201 if err != nil {
202 return []string{}, err
203 }
204 return r.Split(s, n), nil
205 }
206
207 // regexQuoteMeta escapes all regular expression metacharacters in a string.
208 // This is useful when you want to use a string as a literal in a regular expression.
209 //
210 // Parameters:
211 // - s: The string to escape
212 //
213 // Returns:
214 // - string: The escaped string with all regex metacharacters quoted
215 func regexQuoteMeta(s string) string {
216 return regexp.QuoteMeta(s)
217 }