Kotlin Regular Expression (original) (raw)

Last Updated : 12 Jul, 2025

**Regular expressions (regex) are powerful tools used for pattern matching and text manipulation. They are fundamental in almost every modern programming language, and Kotlin is no exception. In Kotlin, regular expression support is provided through the **Regex class. An instance of this class represents a compiled regular expression that can be used for a wide range of string matching operations.

class Regex

We can easily find use of regular expressions in different kind of software, from simplest to incredibly complex applications.

Before preceding this article have a look at Regular Expression in Java

Constructors:

The Regex class provides several constructors to create a regular expression from a pattern string:

  1. **Regex(pattern: String): This constructor creates a regular expression based on the pattern string.
  2. **Regex(pattern: String, option: RegexOption): This constructor creates a regular expression based on the specified pattern and the option. The option is a constant of RegexOption enum class.
  3. **Regex(pattern: String, options: Set): This constructor creates a regular expression on the basis of the specified string pattern and the set of options specified in the set.

Properties:

  1. **val options: Set : It contains the set of options which are to be used at the time of regex creation.
  2. **val pattern: String : It contains the string describing the pattern.

Regex Functions

1. containsMatchIn()

This function returns a boolean indicating whether there exists any match of our pattern in the input.

fun containsMatchIn(input: CharSequence): Boolean

The following is the function to demonstrate the use of **containsMatchIn() function in Kotlin.

Kotlin `

fun main() { // Regex to match any string starting with 'a' val pattern = Regex("^a") println(pattern.containsMatchIn("abc")) println(pattern.containsMatchIn("bac")) }

`

**Output:

true
false

2. find()

This function returns the first matched substring pertaining to our pattern in the input, from the specified
starting index.

fun find(input: CharSequence, startIndex: Int): MatchResult?

The following is the function to demonstrate the use of **find() function in Kotlin.

Kotlin `

fun main() { // Regex to match "ll" in a string val pattern1 = Regex("ll") val ans : MatchResult? = pattern1.find("HelloHello", 5) println(ans ?.value) }

`

**Output:

ll

3. findAll()

This function returns all the matchings of the specified pattern in the input, searching from the given start index.

fun findAll(
input: CharSequence,
startIndex: Int
): Sequence

The following is the function to demonstrate the use of **findAll() function in Kotlin.

Kotlin `

fun main() { // Regex to match a 3 letter pattern beginning with ab val pattern2 = Regex("ab.") val ans1 : Sequence = pattern2.findAll("abcfffgdbabs", 0) // forEach loop used to display all the matches ans1.forEach() { matchResult -> println(matchResult.value) } println() }

`

**Output:

abc
abs

4. matches()

This function returns a boolean indicating whether the input string completely matches the pattern or not.

infix fun matches(input: CharSequence): Boolean

The following is the function to demonstrate the use of **matches() function in Kotlin.

Kotlin `

fun main() { // Tests demonstrating entire string match val pattern = Regex("g([ee]+)ks?") println(pattern.matches("geeks")) println(pattern.matches("geeeeeeeeeeks")) println(pattern.matches("geeksforgeeks")) }

`

**Output:

true
true
false

5. matchEntire()

This function tries to match the entire input to the specified pattern string, returning the string if it matches. If does not match the string, return **null.

fun matchEntire(input: CharSequence): MatchResult?

The following is the function to demonstrate the use of **matchEntire() function in Kotlin.

Kotlin `

fun main() { // Tests demonstrating entire string match var pattern = Regex("geeks?") println(pattern.matchEntire("geeks")?.value) println(pattern.matchEntire("geeeeeeeks")?.value) pattern = Regex("""\D+""") println(pattern.matchEntire("geeks")?.value) println(pattern.matchEntire("geeks12345")?.value) }

`

**Output:

geeks
null
geeks
null

6. replace()

This function replaces all the occurrences of the pattern in the input string with the specified replacement string.

fun replace(input: CharSequence, replacement: String): String

7. replaceFirst()

This function replaces the first match of the regular expression in the input with the replacement string.

fun replaceFirst(
input: CharSequence,
replacement: String
): String

The following is the function to demonstrate the use of **replace() and **replaceFirst() function in Kotlin.

Kotlin `

fun main() { // Tests demonstrating replacement functions val pattern4 = Regex("xyz") // replace all xyz with abc in the string println(pattern4.replace("xyzxyzzzzzzzzz", "abc")) // replace only first xyz with abc not all println(pattern4.replaceFirst("xyzddddddxyz", "abc")) println() }

`

**Output:

abcabczzzzzzzz // replace all
abcddddddxyz // replace first matching only

8. split()

This function breaks the input string into tokens according to the specified parameter.

fun split(input: CharSequence, limit: Int): List

The following is the function to demonstrate the use of **split() function in Kotlin.

Kotlin `

fun main() { // Tests demonstrating split function val pattern = Regex("\s+") // separate for white-spaces val ans : List = pattern.split("This is a sentence") ans.forEach { word -> println(word) } }

`

**Output:

This
is
a
sentence