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
- Character classes, groups, quantifiers and boundaries.
- Predefined classes and groups.
- Greedy quantifiers, reluctant quantifiers and possessive quantifiers.
- Logical operator and escaping a regex pattern.
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:
- SupervillainsClubApplication.kt: The Spring app.
- SupervillainsClubController.kt: The controller which maps the URL paths to the methods serving the requests.
- SupervillainsService.kt: The thin layer between the controller and the model.
- Supervillain.kt: The model of a supervillain.
- 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.
Mobile App Development Best Practices – 21.09
iOS Closures vs. Delegates in Swift iOS How to use the new inspector SwiftUI view modifier How to create an...
2023 App Threat Report
In 2022 alone, the number of mobile app downloads surpassed a staggering 200 billion, emphasizing the pervasive presence of these...
ElectricSQL – Local-first sync layer for web and mobile apps
Local-first sync layer for web and mobile apps. Build reactive, realtime, local-first apps directly on Postgres. What is ElectricSQL? ElectricSQL...
GitHub Copilot Chat opened to individual developers
Like similar chatbots, Copilot Chat sits in the sidebar of the IDE, and developers can use it to have lengthy...
ComposeCard – A beautifully designed payment view library
ComposeCards is a beautifully designed payment view library for Credit and Debit Cards. Made using Jetpack Compose 🎉. It allows...
Mobile App Development Best Practices – 20.09
Airship has released another study of mobile users aka shoppers. A lot of interesting things about habits and tasks in...