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 Trends – 21.02
New Observation Framework, Container Transform Animations, Apple's New Sports App and more!
Roblox paid out $741 million to game creators in 2023
Last year, about a hundred Roblox games brought in more than $1 million, with nine games bringing in at least...
What Windows 12 Mobile could be
It's been nearly a decade since Windows phones effectively ceased to exist, but fans still hold on to the memory...
Vortex – High-performance particle effects for SwiftUI
Vortex is a powerful, high-performance particle system library for SwiftUI, allowing you to create beautiful effects such as fire, rain,...
Mobile App Development Trends – 21.02
Core Data migrations, Refining Compose API, State-of-the-art Open Models and more!
Apple Maps Bottom Sheet
In this video, I’m going to show how to place Modal Sheet over the Tab Bar just like Apple Maps...