Swift Academy · Lesson

RegexBuilder DSL

Compose regexes with a readable builder.

Lesson 3 of 413 steps

RegexBuilder DSL is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

RegexBuilder DSL

Complex regex literals get hard to read. The RegexBuilder module lets you build patterns from Swift code using a result-builder DSL, with named components and types.

Importing RegexBuilder

Import the module, then construct a Regex with the Regex { ... } builder. Inside, each line is a sequential part of the pattern.

import RegexBuilder

let re = Regex {
    "ID-"
    OneOrMore(.digit)
}
print("ID-42".contains(re))

OneOrMore

OneOrMore(...) matches one or more of the given component, equivalent to +. .digit is a built-in character class.

import RegexBuilder

let number = Regex {
    OneOrMore(.digit)
}
if let m = "abc789".firstMatch(of: number) {
    print(m.0)
}

ZeroOrMore and Optionally

ZeroOrMore is * and Optionally is ?. They make parts of the pattern repeatable or skippable.

import RegexBuilder

let re = Regex {
    "color"
    Optionally("u")
    "r"
}
print("colour".wholeMatch(of: re) != nil)
print("color".wholeMatch(of: re) != nil)

Capture

Capture { ... } creates a capturing group. The matched substring appears in the output tuple, just like parentheses in a literal.

import RegexBuilder

let re = Regex {
    "v"
    Capture { OneOrMore(.digit) }
}
if let m = "v123".wholeMatch(of: re) {
    print("Version: \(m.1)")
}

Transforming Captures

A Capture can include a transform closure that converts the matched text into a typed value, so the output is already an Int or other type.

import RegexBuilder

let re = Regex {
    Capture {
        OneOrMore(.digit)
    } transform: { Int($0)! }
}
if let m = "42".wholeMatch(of: re) {
    let n: Int = m.1
    print(n + 1)
}

CharacterClass

Use CharacterClass or built-ins like .word, .whitespace, and ranges like ("a"..."z") to define what characters are allowed.

import RegexBuilder

let re = Regex {
    One(.word)
    ZeroOrMore(("a"..."z"))
}
print("hello".contains(re))

Anchors in the Builder

Add Anchor.startOfLine and Anchor.endOfLine for positional assertions inside the builder.

import RegexBuilder

let re = Regex {
    Anchor.startOfLine
    OneOrMore(.digit)
    Anchor.endOfLine
}
print("100".wholeMatch(of: re) != nil)

Reusable Components

You can store sub-patterns in variables and compose them, which makes large regexes modular and readable.

import RegexBuilder

let field = Reference(Substring.self)
let re = Regex {
    Capture(as: field) { OneOrMore(.word) }
}
if let m = "name".firstMatch(of: re) {
    print(m[field])
}

Multiple Captures

Each Capture adds an output element in order, so a date parser exposes year, month, and day separately.

import RegexBuilder

let date = Regex {
    Capture { Repeat(.digit, count: 4) }
    "-"
    Capture { Repeat(.digit, count: 2) }
}
if let m = "2024-05".wholeMatch(of: date) {
    print("\(m.1) / \(m.2)")
}

Choosing DSL vs Literal

Use literals for short patterns and the DSL for long ones that benefit from names, types, and transforms. They are interchangeable and can even be nested.

import RegexBuilder

let re = Regex {
    "#"
    Capture { /[0-9a-fA-F]{6}/ }
}
if let m = "#FF8800".wholeMatch(of: re) {
    print("Hex: \(m.1)")
}

Quick Check

Which RegexBuilder component matches one or more of something?

Recap

The RegexBuilder DSL builds regexes from Swift code with Regex { }, using OneOrMore, Optionally, Capture with transforms, character classes, and anchors. It makes complex patterns readable and type-safe. Next you will modify text with replacing and splitting.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “RegexBuilder DSL” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “RegexBuilder DSL”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “RegexBuilder DSL”?

Compose regexes with a readable builder. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “RegexBuilder DSL” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Regex Literals and Matching
  2. Capturing Groups
  3. RegexBuilder DSL
  4. Replacing and Splitting Text
← Back to Swift Academy