Regexp The Regexp registry includes functions for pattern matching and string manipulation using regular expressions, providing powerful text processing capabilities.
You can easily import all the functions from the regexp
registry by including the following import statement in your code
Copy import "github.com/go-sprout/sprout/registry/regexp"
regexFind / mustRegexFind
The function returns the first match found in the string that corresponds to the specified regular expression pattern.
Copy RegexFind (regex string , s string ) string
MustRegexFind (regex string , s string ) ( string , error )
Template Example Must version
Copy {{ regexFind "a(b+)" "aaabbb" }} // Output: "abbb"
Copy {{ "hello world" | mustRegexFind "hello" }} // Output: "hello", nil
{{ "hello world" | mustRegexFind "\invalid$^///" }} // Output: "", error
regexFindAll / mustRegexFindAll
The function returns all matches of the regex pattern in the string, up to a specified maximum number of matches (n
).
Copy RegexFindAll (regex string , s string , n int ) [] string
MustRegexFindAll (regex string , s string , n int ) ([] string , error )
Template Example Must version
Copy {{ regexFindAll "a(b+)" "ababab" 2 }} // Output: ["ab", "ab"]
Copy {{ mustRegexFindAll "a." , "aba acada afa" , 3 }} // Output: ["ab", "ac", "af"], nil
{{ mustRegexFindAll "\invalid$^///" , "aba acada afa" , 3 }} // Output: "", error
regexMatch / mustRegexMatch
The function checks if the entire string matches the given regular expression pattern.
Copy RegexMatch (regex string , s string ) bool
MustRegexMatch (regex string , s string ) ( bool , error )
Template Example Must version
Copy {{ regexMatch "^[a-zA-Z]+$" "Hello" }} // Output: true
Copy {{ mustRegexMatch "^[a-zA-Z]+$" , "Hello" }} // Output: true, nil
{{ mustRegexMatch "\invalid$^///" , "Hello" }} // Output: false, error
regexSplit / mustRegexSplit
The function splits the string into substrings based on matches of the regex pattern, performing the split up to n
times.
Copy RegexSplit (regex string , s string , n int ) [] string
MustRegexSplit (regex string , s string , n int ) ([] string , error )
Template Example Must version
Copy {{regexSplit "\\s+" "hello world" - 1 }} // Output: ["hello", "world"]
Copy {{ mustRegexSplit "\\s+" , "hello world from Go" , 2 }} // Output: ["hello", "world from Go"], nil
{{ mustRegexSplit "\invalid$^///" , "hello world from Go" , 2 }} // Output: [], error
regexReplaceAll / mustRegexReplaceAll
The function replaces all occurrences of the regex pattern in the string with the specified replacement string.
Copy RegexReplaceAll (regex string , s string , repl string ) string
MustRegexReplaceAll (regex string , s string , repl string ) ( string , error )
Template Example Must version
Copy {{ regexReplaceAll "[aeiou]" "hello" "i" }} // Output: "hillo"
Copy {{ mustRegexReplaceAll "\\d" , "R2D2 C3PO" , "X" }} // Output: "RXDX CXPO", nil
{{ mustRegexReplaceAll "\invalid$^///" , "R2D2 C3PO" , "X" }} // Output: "", error
regexReplaceAllLiteral / mustRegexReplaceAllLiteral
The function replaces all occurrences of the regex pattern in the string with the specified literal replacement string, without interpreting any special characters in the replacement.
Copy RegexReplaceAllLiteral (regex string , s string , repl string ) string
MustRegexReplaceAllLiteral (regex string , s string , repl string ) ( string , error )
Template Example Must version
Copy {{ regexReplaceAllLiteral "[aeiou]" "hello" "$&" }} // Output: "h$&ll$&"
Copy {{ mustRegexReplaceAllLiteral "world" , "hello world" , "$1" }} // Output: "hello $1", nil
{{ mustRegexReplaceAllLiteral "world" , "hello world" , "\invalid$^///" }} // Output: "", error
regexQuoteMeta
The function returns a version of the provided string that can be used as a literal pattern in a regular expression, escaping any special characters.
Copy RegexQuoteMeta (str string ) string
Template Example
Copy {{ regexQuoteMeta ".+*?^$()[]{}|" }} // Output: "\.\+\*\?\^\$\(\)\[\]\{\}\|"
Last updated 2 months ago