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