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

How to Use regex to match in Kotlin

How to use regex to match in Kotlin

Regular expressions (regex) are a powerful tool for matching patterns in strings. In Kotlin, regex is a part of the standard library, making it easy to use and integrate into your applications. In this guide, we'll explore how to use regex to match patterns in Kotlin, covering the basics, common edge cases, and performance tips.

Quick Example

Here's a minimal example that demonstrates how to use regex to match a pattern in Kotlin:

import kotlin.text.regex.Matcher
import kotlin.text.regex.Pattern

fun main() {
    val input = "Hello, my email is john.doe@example.com"
    val pattern = Pattern.compile("\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b")
    val matcher = pattern.matcher(input)

    if (matcher.find()) {
        println("Email found: ${matcher.group()}")
    } else {
        println("No email found")
    }
}

This code searches for an email address in the input string using a regex pattern.

Step-by-Step Breakdown

Let's break down the code:

  1. import kotlin.text.regex.Matcher and import kotlin.text.regex.Pattern: We import the necessary classes for working with regex in Kotlin.
  2. val pattern = Pattern.compile("\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b"): We define the regex pattern as a string. This pattern matches most common email address formats.
  3. val matcher = pattern.matcher(input): We create a Matcher object from the pattern and the input string.
  4. if (matcher.find()): We use the find() method to search for the first occurrence of the pattern in the input string. If a match is found, we print the matched email address using matcher.group().

Handling Edge Cases

Here are some common edge cases to consider:

Empty/Null Input

If the input string is empty or null, the matcher.find() method will return false. We can add a simple null check to handle this case:

if (input != null && matcher.find()) {
    println("Email found: ${matcher.group()}")
} else {
    println("No email found")
}

Invalid Input

If the input string contains invalid characters, the regex pattern may not match. We can use a try-catch block to handle this case:

try {
    val matcher = pattern.matcher(input)
    if (matcher.find()) {
        println("Email found: ${matcher.group()}")
    } else {
        println("No email found")
    }
} catch (e: Exception) {
    println("Invalid input: $e")
}

Large Input

If the input string is very large, the regex pattern may take a long time to match. We can use the matcher.find(int start) method to search for the pattern starting from a specific index:

val startIndex = 1000
val matcher = pattern.matcher(input)
if (matcher.find(startIndex)) {
    println("Email found: ${matcher.group()}")
} else {
    println("No email found")
}

Unicode/Special Characters

Regex patterns can match Unicode characters and special characters. However, some characters may require special handling. For example, to match a Unicode character, we can use the \u notation:

val pattern = Pattern.compile("\\u00E9") // matches the character é

Common Mistakes

Here are some common mistakes developers make when using regex in Kotlin:

Mistake 1: Incorrect Pattern

  • Wrong code:
    
    

val pattern = Pattern.compile("email")

*   Corrected code:
    ```kotlin
val pattern = Pattern.compile("\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b")
The corrected code uses a regex pattern that matches most common email address formats.

Mistake 2: Missing Import

  • Wrong code:
    
    

val pattern = Pattern.compile("\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b")

*   Corrected code:
    ```kotlin
import kotlin.text.regex.Matcher
import kotlin.text.regex.Pattern

val pattern = Pattern.compile("\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b")
The corrected code includes the necessary import statements.

Mistake 3: Incorrect Method

  • Wrong code:
    
    

val matcher = pattern.matcher(input) if (matcher.matches()) { println("Email found: ${matcher.group()}") } else { println("No email found") }

*   Corrected code:
    ```kotlin
val matcher = pattern.matcher(input)
if (matcher.find()) {
    println("Email found: ${matcher.group()}")
} else {
    println("No email found")
}
The corrected code uses the `find()` method instead of `matches()`.

Performance Tips

Here are some performance tips for using regex in Kotlin:

  1. Use Compile-Time Constants: Compile-time constants can improve performance by reducing the number of regex compilations.
    
    

const val EMAIL_PATTERN = "\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b" val pattern = Pattern.compile(EMAIL_PATTERN)

2.  **Use `find()` instead of `matches()`**: The `find()` method is generally faster than `matches()` because it doesn't require a full match.
    ```kotlin
val matcher = pattern.matcher(input)
if (matcher.find()) {
    println("Email found: ${matcher.group()}")
} else {
    println("No email found")
}
  1. Use Matcher.reset(): If you need to reuse a Matcher object, use the reset() method to reset its state.
    
    

val matcher = pattern.matcher(input) if (matcher.find()) { println("Email found: ${matcher.group()}") } matcher.reset()

FAQ
----

### Q: What is the difference between `find()` and `matches()`?

A: The `find()` method searches for the first occurrence of the pattern in the input string, while the `matches()` method requires a full match of the entire input string.

### Q: How do I match Unicode characters in regex?

A: You can use the `\u` notation to match Unicode characters. For example, `\u00E9` matches the character é.

### Q: What is the performance impact of using regex in Kotlin?

A: The performance impact of using regex in Kotlin depends on the complexity of the regex pattern and the size of the input string. However, Kotlin's regex implementation is generally fast and efficient.

### Q: Can I use regex to parse HTML?

A: No, regex is not recommended for parsing HTML. Instead, use an HTML parsing library like Jsoup or HTMLParser.

### Q: How do I handle null or empty input strings?

A: You can use a null check or an empty string check to handle null or empty input strings. For example:
    ```kotlin
if (input != null && matcher.find()) {
    println("Email found: ${matcher.group()}")
} else {
    println("No email found")
}

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