All Regex functions
Name | Function header | Example | Result | Description | Errorhandling |
---|---|---|---|---|---|
regexFind | func(s string, regex string) string | [[ $result := toString (regexFind "This is a test string" "^This") ]] |
$result = “This” | returns the first (left most) match of the regular expression in the input | |
regexFindAll | func(s string, regex string, n int) []string | [[ $result := toString (regexFindAll "This is a test string" "test" 1) ]] |
$result = “[test]” | returns a slice of all matches of the regular expression in the input string. The last parameter n determines the number of substrings to return, where -1 means return all matches. |
|
regexMatch | func(s string, regex string) bool | [[ $result := toString (regexMatch "This is a test string" "test") ]] |
$result = true | returns true if the input string contains any match of the regular expression. | |
regexReplaceAll | func(s string, regex string, repl string) string | [[ $result := toString (regexReplaceAll "This is a test string" "test" "TesT Replace") ]] |
$result = “This is a TesT Replace string” | returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. | |
regexReplaceAllLiteral | func(s string, regex string, repl string) string | [[ $result := toString (regexReplaceAllLiteral "This is a test string" "test" "TesT Replace") ]] |
$result = “This is a TesT Replace string” | difference to regexReplaceAll: The replacement string is substituted directly, without using Expand | |
regexSplit | func(s string, regex string, n int) []string | [[ $result := toString (regexSplit "This is a test string" "s" 2) ]] |
$result = “[Thi is a test string]” | slices the input string into substrings separated by the expression and returns a slice of the substrings between those expression matches. |
func(s string, regex string) string
The regexFind
function returns the first (left most) match of the regular expression in the input.string.
Examples
Matchs anywhere
[[ $result := toString (regexFind "This is a test string" "test") ]]
$result = "test"
Matchs the beginning
[[ $result := toString (regexFind "This is a test string" "^This") ]]
$result = "This"
Matchs the ending
[[ $result := toString (regexFind "This is a test string" "ing$") ]]
$result = "ing"
[[ $result := toString (regexFind "This is a test string" ".*is....") ]]
$result = "This is a t"
func(s string, regex string, n int) []string
The regexFindAll
function returns a slice of all matches of the regular expression in the input string. The last parameter n determines the number of substrings to return, where -1 means return all matches.
Examples
[[ $result := toString (regexFindAll "This is a test string" "test" 1) ]]
$result = "[test]"
[[ $result := toString (regexFindAll "This is a test string" "^This" 1) ]]
$result = "[This]"
[[ $result := toString (regexFindAll "This is a test string" "ing$" 1) ]]
$result = "[ing]"
[[ $result := toString (regexFindAll "This is a test string" ".*is...." 1) ]]
$result = "This is a t]"
See the difference, if you increase the number
[[ $result := toString (regexFindAll "This is a test string, isn't it" "is" 1) ]]
$result = "[is]"
[[ $result := toString (regexFindAll "This is a test string, isn't it" "is" 2) ]]
$result = "[is is]"
[[ $result := toString (regexFindAll "This is a test string, isn't it" "is" 3) ]]
$result = "[is is is]"
[[ $result := toString (regexFindAll "This is a test string, isn't it" "is" 4) ]]
$result = "[is is is]"
func(s string, regex string) bool
The regexMatch
function returns true if the input string contains any match of the regular expression.
Examples
example true:
[[ $result := toString (regexMatch "This is a test string" "test") ]]
$result = true
[[ $result := toString (regexMatch "This is a test string" "^This") ]]
$result = true
[[ $result := toString (regexMatch "This is a test string" "ing$") ]]
$result = true
[[ $result := toString (regexMatch "This is a test string" ".*is....") ]]
$result = true
example false:
[[ $result := toString (regexMatch "This is a test string" ".*is not....") ]]
$result = false
func(s string, regex string, repl string) string
The regexReplaceAll
function returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. Inside string replacement, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.
Examples
[[ $result := toString (regexReplaceAll "This is a test string" "test" "TesT Replace") ]]
$result = "This is a TesT Replace string"
Example with
[[ $result := toString (regexReplaceAll "This is a test string" "^This is a(.*ng)" "These are${1}s") ]]
$result = "These are test strings"
[[ $result := toString (regexReplaceAll "This is a test string" "ing$" "ingly") ]]
$result = "This is a test stringly"
[[ $result := toString (regexReplaceAll "This is a test string" ".*is...." "Are many t") ]]
$result = "Are many test string"
[[ $result := toString (regexReplaceAll "This is a test string" ".*is not...." "no match, hehehe") ]]
$result = "This is a test string"
func(s string, regex string, repl string) string
The regexReplaceAllLiteral
function returns a copy of the input string, replacing matches of the Regexp with the replacement string replacement. The replacement string is substituted directly, without using Expand
Examples
[[ $result := toString (regexReplaceAllLiteral "This is a test string" "test" "TesT Replace") ]]
$result = "This is a TesT Replace string"
[[ $result := toString (regexReplaceAllLiteral "This is a test string" "^This is a(.*ng)" "These are${1}s") ]]
$result = "These are${1}s"
[[ $result := toString (regexReplaceAllLiteral "This is a test string" "ing$" "ingly") ]]
$result = "This is a test stringly"
[[ $result := toString (regexReplaceAllLiteral "This is a test string" ".*is...." "Are many t") ]]
$result = "Are many test string"
[[ $result := toString (regexReplaceAllLiteral "This is a test string" ".*is not...." "no match, hehehe") ]]
$result = "This is a test string"
[[ $result := toString (regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "${1}") ]]
$result = "a(x*)b"
func(s string, regex string, n int) []string
The regexSplit
function slices the input string into substrings separated by the expression and returns a slice of the substrings between those expression matches.
Application notes / Limits:
The last parameter n determines the number of substrings to return, where -1 means return all matches.
Examples
[[ $result := toString (regexSplit "This is a test string" "s" 2) ]]
$result = "[Thi is a test string]"
[[ $result := toString (regexSplit "This is a test string" "^This" 2) ]]
$result = "[ is a test string]"
[[ $result := toString (regexSplit "This is a test string" "ing$" 2) ]]
$result = "[This is a test str ]"
[[ $result := toString (regexSplit "This is a test string" ".*is...." 2) ]]
$result = "[ est string]"
examples below: increasing the number and what happens
[[ $result := toString (regexSplit "This is a test string, isn't it" "is" 1) ]]
$result = "[This is a test string, isn't it]"
[[ $result := toString (regexSplit "This is a test string, isn't it" "is" 2) ]]
$result = "[Th is a test string, isn't it]"
[[ $result := toString (regexSplit "This is a test string, isn't it" "is" 3) ]]
$result = "[Th a test string, isn't it]"
[[ $result := toString (regexSplit "This is a test string, isn't it" "is" 4) ]]
$result = "[Th a test string, n't it]"