Regular Expressions in Kotlin with Examples: In Practice

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Regular Expressions in Kotlin with Examples: In Practice

\n\n

Regular expressions (regex) are a powerful tool for working with strings. In Kotlin, they are implemented based on the Java classes Pattern and Matcher, but the language adds its own convenient extensions and syntactic sugar. In this article, we will break down how to use regular expressions in Kotlin with real examples: from simple search to replacement and string splitting.

\n\n

Syntax Basics: Creation and Compilation

\n\n

In Kotlin, a regular expression can be created in two ways: via the Regex class or using a string with a pattern. The first method is preferable as it provides more methods.

\n\n
// Method 1: via the Regex class\nval regex = Regex("\\\\d+") // one or more digits\n\n// Method 2: via a string (not recommended for complex expressions)\nval pattern = "\\\\d+".toRegex()
\n\n

Note the double escape slash: \\\\d — this is escaping for Java/Kotlin to get the \\d character in the string. For simplicity, Kotlin supports raw strings (triple quotes """), where escaping is not required:

\n\n
val regex = Regex("""\\d+""") // raw string — backslashes do not need to be duplicated
\n\n

You can also add flags, for example, RegexOption.IGNORE_CASE:

\n\n
val regex = Regex("kotlin", RegexOption.IGNORE_CASE)
\n\n

Searching and Checking Matches

\n\n

The most common operations are to check if a string contains a substring matching a pattern, and to find all matches.

\n\n

Checking for a Match: containsMatchIn

\n\n
val text = "My number: 123-45-678"\nval digitRegex = Regex("""\\d+""")\n\nif (digitRegex.containsMatchIn(text)) {\n    println("The text contains digits!")\n}
\n\n

Finding the First Match: find

\n\n
val result = digitRegex.find(text)\nprintln(result?.value) // Output: 123
\n\n

Finding All Matches: findAll

\n\n
val allMatches = digitRegex.findAll(text)\nallMatches.forEach { match ->\n    println("Found: ${match.value}") // 123, 45, 678\n}
\n\n

The findAll method returns a sequence of MatchResult objects. Each such object contains not only the match itself (.value), but also its position in the string (.range).

\n\n

Working with Capture Groups

\n\n

Capture groups are parts of the pattern enclosed in parentheses. They allow you to extract specific data fragments. For example, let's parse an email into a username and domain:

\n\n
val email = "user@example.com"\nval emailRegex = Regex("""(.+)@(.+)""")\nval matchResult = emailRegex.find(email)\n\nif (matchResult != null) {\n    val (username, domain) = matchResult.destructured\n    println("Username: $username") // user\n    println("Domain: $domain")     // example.com\n}
\n\n

The destructured construct conveniently splits groups into variables. If there are more groups, you can access them by index:

\n\n
val fullMatch = matchResult.value      // user@example.com\nval group1 = matchResult.groupValues[1] // user\nval group2 = matchResult.groupValues[2] // example.com
\n\n

Named Groups (Kotlin 1.7+)

\n\n

Starting from Kotlin 1.7, you can use named groups:

\n\n
val namedRegex = Regex("""(?<name>\\w+)@(?<domain>\\w+\\.\\w+)""")\nval match = namedRegex.find("contact@kotlin.org")\nif (match != null) {\n    println(match.groups["name"]?.value)   // contact\n    println(match.groups["domain"]?.value) // kotlin.org\n}
\n\n

Replacing Text with replace

\n\n

The replace method allows you to replace all matches with a new string. In the replacement pattern, you can reference capture groups wi

Blogs

Book Recommendations