1. Why do you need Regex if you already have replace?
If you already know how to do text.replace("old", "new"), a logical question may come up: “Why do I need another thing called a ‘regular expression’ that gives half of programmers a nervous tic?” And it’s true: for a simple replacement of a specific substring, the usual replace is often enough.
The problem starts when you need to replace not a specific text, but a whole class of similar cases: “any number of spaces,” “any digits,” “any of the characters - or _,” “any tab,” and so on.
Imagine you’re writing a console application (our training CLI), and the user enters a command:
add 120 food coffee
If you try to normalize this “by hand” (remove extra spaces, tabs, newlines), you’ll quickly start writing code that suspiciously resembles a mini-parser. But there’s a simpler way: describe the rule once—“replace all consecutive whitespace characters with a single space”—and you’ll get stable input.
2. What is Regex in Kotlin
Regex as an object and the pattern as a string
In this lecture, we’ll think about Regex in the most down-to-earth way possible. Not as a “language for summoning demons,” but as a pattern that can find something in text by a rule.
A couple of “human-important” points:
Regex in Kotlin is an object. You can create it, store it in a val, and reuse it. The pattern inside Regex(...) is a string with special syntax. There, some characters don’t mean themselves—they mean a “rule.”
For example, \s is “a whitespace character,” and + is “one or more times in a row.” So \s+ reads as “one or more whitespace characters in a row.”
Kotlin provides multiple ways to get a regex; sometimes you’ll see "...".toRegex() in other people’s code. For example, Kotlin documentation shows replacing spaces via input.replace("\\s".toRegex(), ""). But today we keep the focus on the Regex("...") form, to make it maximally explicit: “here is the regex object, and here is its pattern.”
Escaping: regular string vs raw string
With regexes there’s one everyday (and very common) source of pain: escaping.
In Kotlin the string "\\s+" contains two characters \ and s and + (because \\ inside a regular string means “a literal backslash”). And "\s+" is not allowed, because \s would be an attempt to interpret \ as the start of an escape sequence in the string.
So the rule is simple and friendly:
- if you write a regex pattern in a regular string, you will almost always have lots of \\
- if you write a regex pattern in a raw string """...""", you can write backslashes as-is
Let’s compare using one example:
fun main() {
val r1 = Regex("\\s+") // regular string: need \\s
val r2 = Regex("""\s+""") // raw string: write \s as-is
println(r1.pattern) // \s+
println(r2.pattern) // \s+
}
Usually, for regular expressions raw strings are basically “pain-free development mode.” Not a cure for everything, but definitely one less reason to suffer.
3. Regex.replace(...): find everything that matches and replace it
Now the most practical part: the Regex object has a replace method that takes the text and a replacement string. It replaces all matches (i.e., it’s “replace all,” not “replace first”). That’s perfect for normalization.
The basic form looks like this:
val regex = Regex("""...""")
val result = regex.replace(text, "replacement")
And yes, you’ll often see a second style: text.replace(regex, "replacement"). It works too. But today we’ll stick to regex.replace(...), because it reads better as “here’s the rule, let’s apply it to the text.”
4. Whitespace normalization: Regex("""\s+""")
In real input, whitespace comes in different kinds. It’s not only ' ' (a regular space), but also \t (a tab) and a newline \n. For a human it’s “some empty space,” but for parsing a command it’s a potential problem.
The pattern \s means “any whitespace character,” and + means “one or more times in a row.” So \s+ is “any block of whitespace characters.”
Let’s make a function that converts any “dirty” input to “words separated by a single space”:
fun normalizeSpaces(text: String): String {
val spaceRegex = Regex("""\s+""")
return spaceRegex.replace(text.trim(), " ")
}
fun main() {
val raw = " add\t\t120 food \n coffee "
val clean = normalizeSpaces(raw)
println(clean) // add 120 food coffee
}
Notice trim(). The \s+ regex collapses whitespace inside perfectly well, but if the string has “garbage spaces” at the edges, it’s simpler and more readable to remove them with the familiar trim().
5. Mini cheat sheet for regex elements
So we don’t turn this lecture into “an introduction to infinity,” let’s lock in only the regex elements we need today. Important: this is not “everything about regex,” it’s “the minimum to do normalization.”
| Pattern element | Meaning | Example pattern | What it finds |
|---|---|---|---|
|
one whitespace character (space, tab, newline…) | |
any single “space” |
|
one digit | |
|
|
“one or more times in a row” for the previous element | |
a block of spaces/tabs |
|
a set of characters (“one of the listed”) | |
one or |
That’s already enough to bring 80% of “human input” into a normal form.
6. Separator normalization: convert spaces, __, and --- into a single -
Now a real-life example. In our CLI project, we often use expense categories. A user may enter:
Fast Food, fast-food, FAST__FOOD, fast food
But we want to store the category in one style, for example: fast-food.
Let’s build a normalizer that:
- removes spaces at the edges
- converts to lowercase
- collapses any sequences of spaces, hyphens, and underscores into a single hyphen
- removes hyphens at the edges (so you don’t get -food-)
fun normalizeCategory(raw: String): String {
val sep = Regex("""[-_\s]+""") // hyphen or _ or whitespace
val lowered = raw.trim().lowercase()
val dashed = sep.replace(lowered, "-")
return dashed.trim('-')
}
fun main() {
println(normalizeCategory(" FAST__FOOD ")) // fast-food
println(normalizeCategory("fast food")) // fast-food
println(normalizeCategory("__food__")) // food
}
Notice an important point: we don’t make the regex “too smart.” We don’t try to validate “the correctness of the category” down to the last letter. We just bring the input to a single consistent form. That’s normalization: first make the data the same, and only then it becomes easy to compare and group.
7. Data masking: replace digits via \d
Another very practical case is masking data, for example a card/phone number in logs or in a report. Yes, in our training CLI we usually don’t write banking software, but the skill is useful.
The \d pattern finds each digit, and replace(..., "*") replaces each digit with *.
fun maskDigits(text: String): String {
val digit = Regex("""\d""")
return digit.replace(text, "*")
}
fun main() {
val raw = "Order #2048, phone: +1 555 120-77-33"
println(maskDigits(raw))
// Order #****, phone: +* *** ***-**-**
}
This is a simple example, but it shows the mindset well: regex is not necessarily “parse everything.” Sometimes it’s simply “quickly find a class of characters and replace them.”
8. Integrating into our CLI app: resilient command input
Now let’s connect everything to our console project (let’s call it BudgetCLI). As a reminder of the overall style: we read a line, normalize it, parse the command, and update collections.
Let’s say an expense entry is stored for now as Triple<String, Int, String>:
- first — category (already normalized)
- second — amount
- third — note (as text)
Let’s implement a small but illustrative piece: handling the add command.
Normalizing the command line
First, we bring the input to “one space between tokens.” This greatly simplifies split(" ").
fun normalizeCommandLine(raw: String): String {
val spaces = Regex("""\s+""")
return spaces.replace(raw.trim(), " ")
}
fun main() {
val line = " add\t 120 fast__food coffee "
println(normalizeCommandLine(line)) // add 120 fast__food coffee
}
Now the command becomes predictable: you can safely do split(" ") and not fear empty tokens due to five spaces in a row.
Mini handler for add with category normalization
Let’s add one more step: normalize the category via normalizeCategory(...), and assemble the note back into a string.
fun handleAdd(expenses: MutableList<Triple<String, Int, String>>, rawLine: String) {
val line = normalizeCommandLine(rawLine)
val parts = line.split(" ")
val amount = parts.getOrNull(1)?.toIntOrNull() ?: return
val category = normalizeCategory(parts.getOrNull(2) ?: return)
val note = parts.drop(3).joinToString(" ")
expenses.add(Triple(category, amount, note))
}
fun main() {
val expenses = mutableListOf<Triple<String, Int, String>>()
handleAdd(expenses, "add 120 FAST__FOOD coffee to go")
println(expenses) // [(fast-food, 120, coffee to go)]
}
Here we’re not trying to do perfect error handling (you already know how to do that using if, when, try/catch, and validation). But what matters is something else: thanks to Regex.replace, normalization is separated from command logic. You don’t smear “fighting spaces” throughout the code.
Pipeline: raw input → normalized input → parsing
To make it easier for your brain, keep it as a small pipeline:
flowchart LR
A["raw input
user line"] --> B["normalize
Regex.replace + trim"]
B --> C["parse
split + toIntOrNull"]
C --> D["domain update
added to collection"]
The more neatly you separate these steps, the less “magic” you have in one place. And the less magic, the fewer late-night conversations with exceptions.
9. Typical mistakes when working with Regex.replace
Mistake #1: confusing “regular replace” and “regex replace.”
text.replace(".", "-") replaces a dot as a character (if it’s a string), but in regex . means “any character.” If you accidentally think you’re doing a plain string replacement but are actually using a regex, you can replace “too much.” That’s why in this lecture we keep the style val r = Regex(...) and then r.replace(...): it makes it obvious that this is regex logic.
Mistake #2: forgetting about escaping in a regular string.
Regex("\s+") won’t compile the way you expect, because \s inside a regular string is not “literal characters,” but an escape attempt. Either write Regex("\\s+"), or (often more convenient) use a raw string Regex("""\s+""").
Mistake #3: trying to “validate the world” with regex when you need normalization.
A very common beginner trap: instead of collapsing whitespace to one space, a person starts building a regex that “allows only correct input.” As a result, the pattern becomes longer than your program, and the bugs become more interesting. For normalization, simple rules are better: collapsed spaces, normalized case, replaced separators.
Mistake #4: forgetting trim() after whitespace normalization.
Even if you replaced \s+ with " ", a single space may still remain at the edges of the string. It’s not fatal, but later it unexpectedly breaks string comparisons or output. The combination replace(...).trim() usually produces the most predictable result.
Mistake #5: recreating Regex everywhere without need.
Technically, you can write Regex("""\s+""") in every function. But if you do it inside a hot path (for example, in a loop processing thousands of lines), you create unnecessary objects. It’s better to move regexes into a val next to the normalization functions and reuse them. In our training project this is not yet a performance issue, but it is an issue of discipline and code cleanliness.
GO TO FULL VERSION