0Pricing
Swift Academy · Lesson

Replacing and Splitting Text

Transform strings using regex operations.

Replacing and Splitting

Regex is not only for matching. Swift strings can replace matched text and split on a pattern, transforming strings without manual loops.

replacing(_:with:)

replacing(_:with:) returns a new string where every match of the pattern is swapped for the replacement text.

let s = "a1b2c3"
let cleaned = s.replacing(/\d/, with: "*")
print(cleaned)

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