0Pricing
Swift Academy · Lesson

Regex Literals and Matching

Match patterns with the Regex type.

Regex in Swift

Swift 5.7+ has native regular expressions in the standard library and Foundation. You can write a regex as a literal between slashes, like /\d+/, and the compiler checks it at build time.

A Regex Literal

A regex literal produces a Regex value. Its type captures what the match yields. The simplest pattern matches a fixed sequence of characters.

let pattern = /hello/
if "hello world".contains(pattern) {
    print("Found hello")
}

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