Swift Regex Tutorial: Getting Started

Master the pattern-matching superpowers of Swift Regex. Learn to write regular expressions that are easy to understand, work with captures and try out RegexBuilder, all while making a Marvel Movies list app! By Ehab Amer.

4.4 (5) · 2 Reviews

Download materials
Save for later

Searching within text doesn’t always mean searching for an exact word or sequence of characters.

Sometimes you want to search for a pattern. Perhaps you’re looking for words that are all uppercase, words that have numeric characters, or even a word that you may have misspelled in an article you’re writing and want to find to correct quickly.

For that, regular expressions are an ideal solution. Luckily, Apple has greatly simplified using them in Swift 5.7.

In this tutorial, you’ll learn:

  • What a regular expression is and how you can use it.
  • How Swift 5.7 made it easier to work with regular expressions.
  • How to capture parts of the string you’re searching for.
  • How to use RegexBuilder to construct a complex expression that’s easy to understand.
  • How to load a text file that is poorly formatted into a data model.
  • How to handle inconsistencies while loading data.
Note: This tutorial assumes some familiarity with iOS programming, including working with strings. If you’re new, consider starting with a book like SwiftUI Apprentice or a video course like Your First iOS and SwiftUI App: An App From Scratch. That said, feel free to give this tutorial a go and jump into the forum (linked below) to ask questions!

Getting Started

Download the starter project by clicking Download Materials at the top or bottom of the tutorial.

The app you’ll be working on here is MarvelProductions. It shows a list of movies and TV shows that Marvel has already published or announced.

Here’s what you’ll see when you first build and run:

First build and run shows the same movie repeated. You need to fix the data with regular expressions.

You’ll notice that there’s only one repeated entry, which is great for Moon Knight fans but a bit disheartening otherwise. That’s because the app’s data needs some work before it’s ready to display. You’ll use Swift Regex to accomplish this.

Understanding Regular Expressions

Before you get into the app directly, you need to understand what regular expressions, also known as regex, are and how to write them.

Most of the time, when you search for text, you know the word you want to look for. You use the search capability in your text editor and enter the word, then the editor highlights matches. If the word has a different spelling than your search criteria, the editor won’t highlight it.

Regex doesn’t work that way. It’s a way to describe sequences of characters that most text editors nowadays can interpret and find text that matches. The results found don’t need to be identical. You can search for words that have four characters and this can give you results like some and word.

To try things out, open MarvelProductions.xcodeproj file in the starter folder. Then select the MarvelMovies file in the Project navigator.

Xcode open with the file MarvelMovies open

While having the text file focused in Xcode, press Command-F to open the search bar. Click on the dropdown with the word Contains and choose Regular Expression.

Note: If your screen looks messy because lines wrap around, you can toggle this by pressing Control-Shift-Command-L or choosing Wrap Lines from Xcode’s Editor menu.

Selecting Regular Expression search mode

In the search text field, you can still enter a word to search for in the file like you normally would, but now you can do much more. Enter \d in the text field. This will select every digit available in the file.

Xcode highlighting all the digit characters found in the text file

Try to select numbers that aren’t part of the id values that start with tt. Enter the following into the Search field:


Xcode highlighting all the digits in the file except the digits in the id

The regex you just entered matches any digits in a word that don't start with tt. The breakdown of this regex is as follows:

  • Word boundary: \b.
  • Negative lookbehind for tt: (?<!tt).
  • One or more digits: \d+.
Note: To better understand regular expression syntax, consider searching online for some cheat sheets. An Introduction to Regular Expressions is a good starting point. Be sure to check out its playground, included in the downloadable materials.

Swiftifying Regular Expressions

Swift 5.7 introduces a new Regex type that's a first-degree citizen in Swift. It isn't a bridge from Objective-C's NSRegularExpression.

Swift Regex allows you to define a regular expression in three different ways:

  1. As a literal:
  2. let digitsRegex = /\d+/
  3. From a String:
  4. let regexString = #"\d+"#
    let digitsRegex = try? Regex(regexString)
  5. Using RegexBuilder:
  6. let digitsRegex = OneOrMore {
let digitsRegex = /\d+/
let regexString = #"\d+"#
let digitsRegex = try? Regex(regexString)
let digitsRegex = OneOrMore {

The first two use the standard regular expression syntax. What's different about the second approach is that it allows you to create Regex objects dynamically by reading the expressions from a file, server or user input. Because the Regex object is created at runtime, you can't rely on Xcode to check it, which is a handy advantage to the first approach.

The third is the most novel. Apple introduced a new way to define regular expressions using a result builder. You can see it's easier to understand what it's searching for in the text. An arguable drawback is that this approach is more verbose.

Now it's time to see Swift Regex in action. Open the Project navigator and select the file ProductionsDataProvider.swift.

ProductionsDataProvider.swift open in Xcode

Loading the Marvel Movies List

As you can see, the data provider only loads a few sample objects and isn't loading the data from the MarvelMovies file. You'll use regular expressions to find the values and load them into an array of MarvelProductionItem objects. You might wonder, "Why do I need to use regular expressions to load the file? It looks clear, and I can separate it with normal string operations."

MarvelMovies file open in Xcode

The answer is "looks can be deceiving". The file looks organized to the human eye, but that doesn't mean the data itself is organized.

If you look closely, empty spaces separate the fields. This space can be two or more space characters, one or more tab characters or a collection of both of them together.

Using usual string splitting is possible if separators are explicit and unique, but in this case, the separator string varies. Also, spaces appear in the content, making it hard to use conventional means to break down each line to parse the values. Regex is ideal here!