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
The function returns the first match found in the string that corresponds to the specified regular expression pattern.
Template Example
Copy {{ "hello world" | regexFind "hello" }} // Output: "hello", nil
{{ "hello world" | regexFind "\invalid$^///" }} // Error
regexFindAll
The function returns all matches of the regex pattern in the string, up to a specified maximum number of matches (n
).
Template Example
Copy {{ regexFindAll "a." , "aba acada afa" , 3 }} // Output: ["ab", "ac", "af"], nil
{{ regexFindAll "\invalid$^///" , "aba acada afa" , 3 }} // Error
regexMatch
The function checks if the entire string matches the given regular expression pattern.
Template Example
Copy {{ regexMatch "^[a-zA-Z]+$" , "Hello" }} // Output: true, nil
{{ regexMatch "\invalid$^///" , "Hello" }} // Output: false, error
regexSplit
The function splits the string into substrings based on matches of the regex pattern, performing the split up to n
times.
Template Example
Copy {{ mustRegexSplit "\\s+" , "hello world from Go" , 2 }} // Output: ["hello", "world from Go"], nil
{{ mustRegexSplit "\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.
Template Example
Copy {{ regexReplaceAll "\\d" , "R2D2 C3PO" , "X" }} // Output: "RXDX CXPO", nil
{{ regexReplaceAll "\invalid$^///" , "R2D2 C3PO" , "X" }} // Output: "", 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.
Template Example
Copy {{ regexReplaceAllLiteral "world" , "hello world" , "$1" }} // Output: "hello $1", nil
{{ regexReplaceAllLiteral "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.
Template Example
Copy {{ regexQuoteMeta ".+*?^$()[]{}|" }} // Output: "\.\+\*\?\^\$\(\)\[\]\{\}\|"
regexFindGroups
The function finds the first match of a regex pattern in a string and returns the matched groups, with error handling.
Template Example
Copy {{ "aaabbb" | regexFindGroups "(a+)(b+)" }} // Output: ["aaabbb", "aaa", "bbb"], nil
{{ "aaabbb" | regexFindGroups "\invalid$^///" }} // Error
regexFindAllGroups
The function finds all matches of a regex pattern in a string up to a specified limit and returns the matched groups, with error handling.
Template Example
Copy {{ "aaabbb aab aaabbb" | regexFindAllGroups "(a+)(b+)", -1 }} // Output: [["aaabbb", "aaa", "bbb"], ["aab", "aa", "b"], ["aaabbb", "aaa", "bbb"]], nil
{{ "aaabbb aab aaabbb" | regexFindAllGroups "(a+)(b+)" , 1 }} // Output: [["aaabbb", "aaa", "bbb"]], nil
{{ "aaabbb" | regexFindAllGroups "\invalid$^///" }} // Error
regexFindNamed
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.
Template Example
Copy {{ "aaabbb" | regexFindNamed "(?P<first>a+)(?P<second>b+)" }} // Output: map["first":"aaa", "second":"bbb"], nil
{{ "aaabbb" | regexFindNamed "(?P<first>a+)(b+)" }} // Output: map["first":"aaa"], nil
{{ "bbb" | regexFindNamed "(?P<first>a+)" }} // Output: map[], nil
{{ "aaabbb" | regexFindNamed "\invalid$^///" }} // Error
regexFindAllNamed
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.
Template Example
Copy {{ "var1=value1&var2=value2" | regexFindAllNamed "(?P<param>\\w+)=(?P<value>\\w+)" -1 }} // Output: [map[param:var1 value:value1] map[param:var2 value:value2]], nil
{{ "var1=value1&var2=value2" | regexFindAllNamed "(?P<param>\\w+)=(?P<value>\\w+)" 1 }} // Output: [map[param:var1 value:value1]], nil
{{ "var1+value1" | regexFindAllNamed "(?P<param>\\w+)=(?P<value>\\w+)" - 1 }} // Output: map[], nil
{{ "var1=value1" | regexFindAllNamed "\invalid$^///" - 1 }} // Error