Connect with us


Regular expressions in Kotlin

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 classesgroupsquantifiers and boundaries.
  • Predefined classes and groups.
  • Greedy quantifiers, reluctant quantifiers and possessive quantifiers.
  • Logical operator and escaping a regex pattern.

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.

Full Article: arjuna sky kok @
Mar 14, 2022