0Pricing
Swift Academy · Lesson

RegexBuilder DSL

Compose regexes with a readable builder.

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))

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