Kotlin Regex

Regular expressions are an important aspect of any programming language, and kotlin is one of them. The regex class in Kotlin provides support for regular expressions. Regular expressions can be found in various software applications, from the simplest to the most complicated.

What is the Regex() function in Kotlin?

The kotlin functions are similar to methods and classes used to write an expression and variable text data, as well as funding and enhanced content for development. All characters, including symbols, operators, alphabets, and non-alphabets, are likely to be supported by the refer function in kotlin.

Constructor of Regex() function in Kotlin?

(pattern: String): The pattern string is utilized to create regular expressions with this constructor.

(pattern: String, option: RegexOption): The constructor specified the pattern and the option-based regular expression. The regex option enumerated class has a single constant option.

(pattern: String, options: Set<RegexOption>): The constructor creates a regular expression from the given string pattern as well as sets the option.

How to use the Kotlin Regex() function?

To understand the basics of using the regex functions in Kotlin language, you should take a look at the following examples:

Example # 1: Using Regex MatchIn() function in Kotlin:

The function conatinMatchIn produces a Boolean value that indicates whether the input has any matches for our pattern string.

The main function has a string pattern defined in a variable “strPattern.” The “strPattern” takes the regex function, and the regex function has a navigation “^” which matches the string from the starting point. We have passed a character “k” in the regex function with navigation “^.” The regex matches the given string starting with “k.” Then, we have the kotlin println function, which has MatchIn() function. The MatchIn function is defined with the string “kotlin” and “object-oriented” separately. The output returns from the matchIn() function on the console screen of ubuntu.

Example # 2: Using Regex Find() and FindAll() function in Kotlin:

From the indicated starting index, the find() function will return the very first match substring corresponding to the given pattern string in the input.

We have a declaration of the main function. The main function has a variable as the “val” keyword and is defined as “myPattern.” The variable “myPattern” has the regex function, containing a two-character “le.” Then, we have another variable defined as “myResult,” which only matches the preceding element once as we have used the “?” operator. The variable “myResult” has called the find() function, which will find the specified character “le” from the given string. The find() function will start from the index “2”. The println function will return the string pattern if any single string matches at first.

Hence, the output shows that we have matched the string pattern in the input and returned the character “le” from the given string.

The findAll() function searches the input for all matches of the provided pattern string that start at the start index given in the function and return all of them.

In the above code, we have a variable as “myPattern” and initialize it with the regex function. The regex function contains an expression that will match the three characters beginning with “be.” Then, we have another variable that uses the “findAll()” function and finds the given character that appears as many times in the whole string. The “findAll()” function will start from the index “1” as specified. The for each() technique is used to print the matches of the pattern string.

We have the output of matched pattern from the specified string as presented below.

Example # 3: Using Regex Matches() and MatchEntire() function in Kotlin:

The matches() function provide a Boolean value that indicates whether the input string matches the pattern completely or not.

In the above example code, we have taken a variable as “regex_pattern, ” subsequently initialized with the regex function. The regex function has a pattern string passed. We have kept the letter “ll” in the square brackets in the pattern string, which will match within the “ll” letter. We have also used the “+” operator with the square brackets. The + operator will help match the subsequent values one or more times. Then, the kotlin println function is passed with the matches function(). The matches() function contains a string that will be matched with the regex pattern.

The output has boolean values according to the matched pattern from the string input shown on the terminal screen.

The function matchEntire() compares the entire input to a defined pattern string. If it matches, it will return the string or else return null the string does not match.

In the above code, we have demonstrated the entire string match. The variable “myRegex” has been initialized with the pattern “hello” along with the question mark “?” which matches the preceding value only once. Then, we have a println function for printing matchEntire() function execution. The matchEntire() function has a string that will match the pattern string. It will return “null” if the string does not match the pattern input string or the string pattern.

The entire matched pattern or not matched pattern output is displayed on the terminal screen.

Example # 4: Using Regex Replace() and ReplaceFirst() function in Kotlin:

The replaceAll() function substitutes the given replacement string for all occurrences of the input string pattern. The replaceFirst() function, on the other hand, substitutes the replacement string of the regular expression’s first occurrence in the given input string pattern.

We have a regex function in the above code that contains a pattern string “scala.” The regex function is initialized in a “newPattern” variable. Then, we have the kotlin println function, which will take the replace() function and replaceFirst() function individually. If the pattern string matches, the replace() function will replace the entire string with the kotlin. On the other hand, the replaceFirst() string will replace the first occurrence of “scala” with “kotlin,” not all the strings.

As shown in the output that the replace() function replaces the pattern “scala” with the kotlin where it found the “scala” pattern on the whole given string, and the replaceFirst() function only replaces the first pattern found in the string given.


The article was aimed to teach the usage of the regex function in Kotlin using Ubuntu 20.04. Regex Function in Kotlin is helpful. This will make our work easier as we can find anything from the large content. We have different example codes of the regex function. These examples will help you deal with regex pattern sequences in the Kotlin programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.