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
- Regex Literals and Matching
- Capturing Groups
- RegexBuilder DSL
- Replacing and Splitting Text