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

function matches(string stringToMatch, string regex) returns boolean
Isolated Function

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 ]+");

Parameters

  • stringToMatch string
  • The string to match the regex

  • regex string
  • The regex to match the string

Return Type

(boolean)

true if the provided string matches the regex or else false

replace

function replace(string originalString, string regex, Replacement replacement, int startIndex) returns string
Isolated Function

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

Return Type

(string)

The resultant string with the replaced substring

replaceAll

function replaceAll(string originalString, string regex, Replacement replacement) returns string
Isolated Function

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

Return Type

(string)

The resultant string with the replaced substrings

replaceFirst

function replaceFirst(string originalString, string regex, string replacement) returns string
Deprecated
Isolated Function

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.

function search(string str, string regex, int startIndex) returns Match?
Isolated Function

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]*");

Parameters

  • str string
  • The string to be matched

  • startIndex int (default 0)
  • The starting index for the search

Return Type

(Match?)

a Match record which holds the matched substring, or nil if there is no match

searchAll

function searchAll(string str, string regex) returns Match[ ]
Isolated Function

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]*");

Parameters

  • str string
  • The string to be matched

Return Type

(Match[ ])

An array of Match records

split

function split(string receiver, string delimiter) returns string[ ]
Isolated Function

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", " ");

Parameters

  • receiver string
  • The string to split

  • delimiter string
  • The delimiter is a regex, which splits the given string

Return Type

(string[ ])

An array of strings containing the individual strings that are split