Try it yourself with our free Regex Tester tool — runs entirely in your browser, no signup needed.

How to Use regex to replace in Kotlin

How to use regex to replace in Kotlin

Using regular expressions (regex) to replace text is a common task in programming. In Kotlin, you can use the java.util.regex package to achieve this. Regex replacement is useful when you need to perform complex text manipulations, such as replacing patterns in a string. In this guide, we will explore how to use regex to replace text in Kotlin, covering the basics, handling edge cases, and providing performance tips.

Quick Example

import java.util.regex.Pattern

fun main() {
    val text = "Hello, world! world!"
    val regex = "world"
    val replacement = "Kotlin"
    val result = text.replace(Regex(regex), replacement)
    println(result) // Output: "Hello, Kotlin! Kotlin!"
}

This example replaces all occurrences of "world" with "Kotlin" in the given text.

Step-by-Step Breakdown

Let's break down the example code:

import java.util.regex.Pattern

We import the java.util.regex.Pattern class, which provides the Regex class we will use for regex operations.

val text = "Hello, world! world!"

We define a sample text string.

val regex = "world"

We define the regex pattern to match. In this case, we're matching the literal string "world".

val replacement = "Kotlin"

We define the replacement string.

val result = text.replace(Regex(regex), replacement)

We use the replace function to replace all occurrences of the regex pattern in the text with the replacement string. The Regex class is used to compile the regex pattern.

println(result) // Output: "Hello, Kotlin! Kotlin!"

We print the result, which shows the replaced text.

Handling Edge Cases

Empty/null input

When dealing with empty or null input, you should add a null check to avoid a NullPointerException.

fun replaceText(text: String?, regex: String, replacement: String): String? {
    return text?.replace(Regex(regex), replacement)
}

Invalid input

If the regex pattern is invalid, the Regex constructor will throw a PatternSyntaxException. You can catch this exception to handle invalid input.

fun replaceText(text: String, regex: String, replacement: String): String {
    try {
        return text.replace(Regex(regex), replacement)
    } catch (e: PatternSyntaxException) {
        // Handle invalid regex pattern
        return text
    }
}

Large input

For large input strings, you can use the replaceAll function with a Matcher object to improve performance.

fun replaceText(text: String, regex: String, replacement: String): String {
    val pattern = Pattern.compile(regex)
    val matcher = pattern.matcher(text)
    return matcher.replaceAll(replacement)
}

Unicode/special characters

When dealing with Unicode or special characters, make sure to use the correct character encoding.

fun replaceText(text: String, regex: String, replacement: String): String {
    val pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS)
    return text.replace(pattern, replacement)
}

Common Mistakes

1. Not compiling the regex pattern

Wrong code:

val result = text.replace(regex, replacement)

Corrected code:

val result = text.replace(Regex(regex), replacement)

Explanation: The replace function does not compile the regex pattern. You need to use the Regex class to compile the pattern.

2. Not handling null input

Wrong code:

val result = text.replace(Regex(regex), replacement)

Corrected code:

val result = text?.replace(Regex(regex), replacement)

Explanation: If the input text is null, a NullPointerException will be thrown. Add a null check to avoid this.

3. Not handling invalid regex patterns

Wrong code:

val result = text.replace(Regex(regex), replacement)

Corrected code:

try {
    val result = text.replace(Regex(regex), replacement)
} catch (e: PatternSyntaxException) {
    // Handle invalid regex pattern
}

Explanation: If the regex pattern is invalid, a PatternSyntaxException will be thrown. Catch this exception to handle invalid input.

Performance Tips

1. Compile the regex pattern only once

If you're using the same regex pattern multiple times, compile it only once to improve performance.

val regex = Regex("world")
val result1 = text1.replace(regex, replacement)
val result2 = text2.replace(regex, replacement)

2. Use the replaceAll function with a Matcher object

For large input strings, use the replaceAll function with a Matcher object to improve performance.

val pattern = Pattern.compile(regex)
val matcher = pattern.matcher(text)
return matcher.replaceAll(replacement)

3. Use the UNICODE_CHARACTER_CLASS flag

When dealing with Unicode or special characters, use the UNICODE_CHARACTER_CLASS flag to improve performance.

val pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS)
return text.replace(pattern, replacement)

FAQ

Q: What is the difference between replace and replaceAll?

A: The replace function replaces all occurrences of the regex pattern, while the replaceAll function replaces all occurrences of the regex pattern and also returns a Matcher object.

Q: How do I handle null input?

A: Use a null check to avoid a NullPointerException.

Q: How do I handle invalid regex patterns?

A: Catch the PatternSyntaxException to handle invalid regex patterns.

Q: How do I improve performance for large input strings?

A: Use the replaceAll function with a Matcher object.

Q: How do I handle Unicode or special characters?

A: Use the UNICODE_CHARACTER_CLASS flag when compiling the regex pattern.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp