Regular Expressions in Kotlin

Learn how to improve your strings manipulation with the power of regular expressions in Kotlin. You’ll love them! By arjuna sky kok.

4.7 (3) · 1 Review

Download materials
Save for later
Share

String manipulation and validation are common operations you’ll encounter when writing Kotlin code. For example, you may want to validate a phone number, email or password, among other things. While the string class has methods for modifying and validating strings, they’re not enough.

For example, you can use endsWith to check whether a string ends with numbers. However, there are cases where using only simple string methods could make you write more code than needed.

Imagine you want to check whether a string is a phone number. The pattern for a phone number is three number strings separated by dashes. For example, 333-212-555 is a phone number, but 3333/2222 isn’t.

In this case, using string methods would work but could be very cumbersome. You’d need to split the string then check whether each result is a number string.

Regular expressions, or regex, are tools that can help you solve string validation and string manipulation problems in a more compact way.

In this tutorial, you’ll learn:

  • How to create a regex pattern string and a Regex object.
  • Regex‘s methods.
  • Character classes, groups, quantifiers and boundaries.
  • Predefined classes and groups.
  • Greedy quantifiers, reluctant quantifiers and possessive quantifiers.
  • Logical operator and escaping a regex pattern.
Note: This tutorial assumes you know the basics of Kotlin and how to use IntelliJ IDEA. If you’re new to IntelliJ IDEA, read IntelliJ IDEA’s installation guide.
If you’re new to Kotlin, check out this Kotlin Apprentice book. For more information about Spring web development, you can read Kotlin and Spring Boot: Getting Started.

Getting Started

Download the materials using the Download Materials button at the top or bottom of this tutorial. Open IntelliJ IDEA and import the starter project.

Before you start working, take a moment to learn the app’s backstory.

Understanding the Backstory

Superheroes are becoming more dominant and popular, which worries supervillains. So they decided to join the forces to build Supervillains Club, a club dedicated to advancing the interest of villains and recruiting people to become supervillains. With more supervillains going for retirement soon, they need fresh blood.

Supervillains Club has a rudimentary web app, but it’s not sufficient. Right now, anyone can register in the web app, including superheroes masquerading as supervillains. The web app needs string validations to filter them and other features that require string methods.

You just accepted a job as a software engineer at Supervillains Club. Your task is to write functions using regex to validate, modify and process strings. By your work, supervillains will rise to glory!

But first, you need to familiarize yourself with the web app, a Spring web application. Take a moment to examine the code.

The main files are:

  1. SupervillainsClubApplication.kt: The Spring app.
  2. SupervillainsClubController.kt: The controller which maps the URL paths to the methods serving the requests.
  3. SupervillainsService.kt: The thin layer between the controller and the model.
  4. Supervillain.kt: The model of a supervillain.
  5. RegexValidator.kt: The core library that processes strings with regex. This is where you’ll write your code.

There are also test files in the tests directory, SupervillainsClubControllerTest.kt and RegexValidatorTest.kt.

Building and Running the Web App

Build and run the web app. Then open http://localhost:8080. You’ll see the welcome screen and read the Supervillains Club mission:

Supervillains Club Landing Page

Click Register to enter the signup page. You’ll see a form asking for your name and description:

Supervillains Club Register Page

Choose batman for Name and A superhero from DC. for Description. Then click Register. You’ll see the following screen:

Supervillains Club List Page

Mayday, mayday: batman infiltrated Supervillains Club!

You need to find a solution for that as soon as possible!

It’s tempting to check whether a particular string is batman. But you got a requirement to prevent batman, batwoman, catman and catwoman from infiltrating Supervillains Club. It has to take case-insensitivity into account, so Batman is a no-go as well.

You may think of this solution:

fun validateName(name: String): Boolean {
  val lowerName = name.toLowerCase()
  if (lowerName=="batman" || lowerName=="batwoman" || lowerName=="catman" || lowerName=="catwoman") {
      return false
  }
  return true
}

That works, but it’s not scalable. What if you want to prevent strings with stretched vowels, like batmaaan and batmaaaaaaaaaaaaaaan? The if condition swould be too unmanageable.

It’s time to put regex to good use: preventing superheroes from entering Supervillains Club. :]

The Regex Object

Regex pattern is a string with a particular syntax and has rules. It’s complex and confusing, even for veterans. In this tutorial, you’ll learn common rules that cover 80% of your daily needs.

A regex string can be like a normal string, for example, batman. But you can’t do anything with this string. You have to convert it into a Regex object.

To convert this regex string into a Regex object, you feed the string as the argument to the Regex class constructor:

val pattern = Regex("batman")

Now, you have a Regex object.

Regex has a couple of methods. The method that’s suitable for your purpose is containsMatchIn.

Find validateName in RegexValidator.kt. Replace the comment // TODO: Task 1 with:

val pattern = Regex("batman")
if (pattern.containsMatchIn(name)) {
  return false
}

containsMatchIn returns true if batman is in the name variable.

Build and run the app. Open http://localhost:8080/register. Fill batman for Name and A superhero from DC. for Description, then click Register:

Supervillains Club Validation on Register Page

Nice! batman can’t enter Supervillains Club. However, if you try with Batman, your validation will fail.

You’ll use RegexOption to solve that problem.

Using RegexOption

You can make regex case-insensitive by passing RegexOption.IGNORE_CASE as the second parameter to Regex‘s constructor. Replace Regex(“batman”) with:

val pattern = Regex("batman", RegexOption.IGNORE_CASE)

Build and rerun the app. Choose Batman for Name then submit the form.

This time, the validation works perfectly. You finally prevented Batman from entering Supervillains Club for good.

Supervillains Club Batman with Capital B Validation

You can use more than one RegexOption. Can you guess how? :]

[spoiler title=”Solution”]
You can pass a set of RegexOptions instead of a single value:

[/spoiler]

You can read more about RegexOption in the official documentation.

Note: Along with RegexOption.IGNORE_CASE you may have other regex options.
val pattern = Regex("batman", setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))
val pattern = Regex("batman", setOf(RegexOption.IGNORE_CASE, RegexOption.MULTILINE))