RegexBuilder DSL
Compose regexes with a readable builder.
RegexBuilder DSL is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “RegexBuilder DSL” lesson free?
Yes — the full text of “RegexBuilder DSL” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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.