Regular Expressions in Swift

Use regular expressions to pattern-match and extract data from strings in Swift.

Regular Expressions in Swift
Photo by Sergey Zolkin / Unsplash

Use regular expressions to pattern-match and extract data from strings in Swift.

import Foundation
let input = "My email is example@email.com"
let pattern = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
if let range = input.range(of: pattern, options: .regularExpression) {
    let email = input[range]
    print("Found email: \(email)")
}