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
The function splits the string into substrings based on matches of the regex pattern, performing the split up to n times.
Signature
RegexSplit(regex string, s string, n int) ([]string, error)
{{ regexSplit "\\s+" "hello world from Go" 2 }}
// Output(humain readable): [hello "world from Go"]
{{ regexSplit "\\invalid$^///" "hello world from Go" 2 }} // Error
regexReplaceAll
The function replaces all occurrences of the regex pattern in the string with the specified replacement string.
Signature
RegexReplaceAll(regex string, s string, repl string) (string, error)
{{ regexReplaceAll "\\d" "R2D2 C3PO" "X" }} // Output: "RXDX CXPO"
{{ regexReplaceAll "\\invalid$^///" "R2D2 C3PO" "X" }} // Error
regexReplaceAllLiteral
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.
Signature
RegexReplaceAllLiteral(regex string, s string, repl string) (string, error)
The function finds the first match of a regex pattern with named capturing groups in a string and returns a map of group names to matched strings, with error handling.
The function finds all matches of a regex pattern with named capturing groups in a string up to a specified limit and returns a slice of maps of group names to matched strings, with error handling.
Signature
RegexFindAllNamed(regex string, n int, str string) ([]map[string]string, error)