regex.go 7.4 KB

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