Functions
matches | I Checks whether the given string matches the provided regex. |
replace | I Replaces the first substring that matches the given regex with the provided replacement string or string returned by the provided function. |
replaceAll | I Replaces each occurrence of the substrings, which match the provided regex from the given original string value with the provided replacement string. |
replaceFirst | D I Replaces the first substring that matches the given regex with the provided replacement string. |
search | I Returns the first substring in str that matches the regex. |
searchAll | I Returns all substrings in string that match the regex. |
split | I Returns an array of strings by splitting a string using the provided regex as the delimiter. |
matches
Checks whether the given string matches the provided regex.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1boolean isMatched = regex:matches("Ballerina is great", "Ba[a-z ]+");
replace
function replace(string originalString, string regex, Replacement replacement, int startIndex) returns string
Replaces the first substring that matches the given regex with
the provided replacement string or string returned by the provided function.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1string result = regex:replace("Ballerina is great", "\\s+", "_");
Parameters
- originalString string
The original string to replace the first occurrence of the substring that matches the provided regex
- regex string
The regex to match the first substring in the originalString
to be replaced
- replacement Replacement
The replacement string or a function to be invoked to create the new substring to be used to replace the first match to the given regex
- startIndex int (default 0)
The starting index for the search
replaceAll
function replaceAll(string originalString, string regex, Replacement replacement) returns string
Replaces each occurrence of the substrings, which match the provided
regex from the given original string value with the provided replacement string.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1string result = regex:replaceAll("Ballerina is great", "\\s+", "_");
Parameters
- originalString string
The original string to replace the occurrences of the substrings that match the provided regex
- regex string
The regex to match the substrings in the originalString
, which is to be replaced
- replacement Replacement
The replacement string or a function to be invoked to create the new substring to be used to replace the first match to the given regex
replaceFirst
Replaces the first substring that matches the given regex with
the provided replacement string.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1string result = regex:replaceFirst("Ballerina is great", "\\s+", "_");
Parameters
- originalString string
The original string to replace the first occurrence of the substring that matches the provided regex
- regex string
The regex to match the first substring in the originalString
to
be replaced
- replacement string
The replacement string to replace the first substring, which matches the regex
Return Type
(string)The resultant string with the replaced substring
Deprecated
This function will be removed in a later. Use replace
instead.
search
Returns the first substring in str that matches the regex.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1regex:Match? result = regex:search("Betty Botter bought some butter but she said the butter’s bitter.",2 "\\b[bB].tt[a-z]*");
searchAll
Returns all substrings in string that match the regex.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1regex:Match[] result = regex:searchAll("Betty Botter bought some butter but she said the butter’s bitter.",2 "\\b[bB].tt[a-z]*");
split
Returns an array of strings by splitting a string using the provided
regex as the delimiter.
Note that \\
is used as for escape sequence and \\\\
is used to inserts a backslash
character in the string or regular expression.
1string[] result = regex:split("Ballerina is great", " ");